Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable Raw fields are not handled well for OAS 3.1.0 #960

Open
tsokalski opened this issue Dec 6, 2024 · 0 comments · May be fixed by #961
Open

Nullable Raw fields are not handled well for OAS 3.1.0 #960

tsokalski opened this issue Dec 6, 2024 · 0 comments · May be fixed by #961

Comments

@tsokalski
Copy link
Contributor

When converting a nullable Raw Marshmallow field into its corresponding OpenAPI representation for OAS 3.1.0, the generated representation does not match the behaviour of the actual schema/field:

import jsonschema
from marshmallow import Schema, fields

from apispec import APISpec
from apispec.ext.marshmallow import OpenAPIConverter


class TestSchema(Schema):
    raw_field = fields.Raw(allow_none=True)


spec = APISpec(title="Pets", version="0.1", openapi_version="3.1.0")
converter = OpenAPIConverter(
    openapi_version="3.1.0", schema_name_resolver=None, spec=spec
)
json_schema = converter.schema2jsonschema(TestSchema)
data = {"raw_field": "test"}
TestSchema().load(data)  # Works, as expected
jsonschema.validate(data, json_schema)  # Fails unexpectedly

The output of printing the json_schema variable above is the following:

{'type': 'object', 'properties': {'raw_field': {'type': ['null']}}}

It appears the FieldConverterMixin's field2nullable is appending null to the list of supported types for OAS 3.1.0, which makes sense in many cases, but when there's no pre-existing type value defined, the result is just ['null'], which means that the field can only be null. This doesn't match the behaviour of the defined schema.

I've implemented a workaround for this where I've overridden the DEFAULT_FIELD_MAPPING for marshmallow.fields.Raw to map to all supported JSON types (["boolean", "object", "array", "number", "string"]), but it's a bit of a hack, and would be nice if the library could support this a bit better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant