Skip to content

Openapi 3.0 unmarshalling None with nullable subschema fix #876

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ def _unmarshal_properties(
class MultiTypeUnmarshaller(PrimitiveUnmarshaller):
def __call__(self, value: Any) -> Any:
primitive_type = self.schema_validator.get_primitive_type(value)
# OpenAPI 3.0: handle no type for None
if primitive_type is None:
return None
unmarshaller = self.schema_unmarshaller.get_type_unmarshaller(
primitive_type
)
Expand Down Expand Up @@ -247,6 +250,9 @@ def unmarshal(self, value: Any) -> Any:
schema_type = self.schema.getkey("type")
type_unmarshaller = self.get_type_unmarshaller(schema_type)
typed = type_unmarshaller(value)
# skip finding format for None
if typed is None:
return None
schema_format = self.find_format(value)
if schema_format is None:
return typed
Expand Down
1 change: 1 addition & 0 deletions openapi_core/validation/schemas/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def get_primitive_type(self, value: Any) -> Optional[str]:
continue
assert isinstance(schema_type, (str, type(None)))
return schema_type
# OpenAPI 3.0: None is not a primitive type so None value will not find any type
return None

def iter_valid_schemas(self, value: Any) -> Iterator[SchemaPath]:
Expand Down
38 changes: 38 additions & 0 deletions tests/integration/unmarshalling/test_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,25 @@ def test_object_property_nullable(self, unmarshallers_factory):

assert result == value

def test_subschema_nullable(self, unmarshallers_factory):
schema = {
"oneOf": [
{
"type": "integer",
},
{
"nullable": True,
},
]
}
spec = SchemaPath.from_dict(schema)
unmarshaller = unmarshallers_factory.create(spec)
value = None

result = unmarshaller.unmarshal(value)

assert result is None


class TestOAS30RequestSchemaUnmarshallersFactory(
BaseTestOASSchemaUnmarshallersFactoryCall,
Expand Down Expand Up @@ -2086,3 +2105,22 @@ def test_any_null(self, unmarshallers_factory):
result = unmarshaller.unmarshal(None)

assert result is None

def test_subschema_null(self, unmarshallers_factory):
schema = {
"oneOf": [
{
"type": "integer",
},
{
"type": "null",
},
]
}
spec = SchemaPath.from_dict(schema)
unmarshaller = unmarshallers_factory.create(spec)
value = None

result = unmarshaller.unmarshal(value)

assert result is None
Loading