Skip to content

Commit

Permalink
multi types schema format unmarshal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Apr 24, 2023
1 parent b91e977 commit 05da0e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
16 changes: 9 additions & 7 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,10 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
return value
try:
return format_unmarshaller(value)
except (ValueError, TypeError) as exc:
raise FormatUnmarshalError(value, schema_format, exc)
except (ValueError, TypeError):
return value

def get_unmarshaller(
self, schema_format: str
) -> Optional[FormatUnmarshaller]:
def get_unmarshaller(self, schema_format: str) -> Optional[FormatUnmarshaller]:
if schema_format in self.custom_formatters:
formatter = self.custom_formatters[schema_format]
return formatter.format
Expand Down Expand Up @@ -316,8 +314,12 @@ def evolve(self, schema: Spec) -> "SchemaUnmarshaller":

def find_format(self, value: Any) -> Optional[str]:
for schema in self.iter_valid_schemas(value):
if "format" in schema:
return str(schema.getkey("format"))
if "format" not in schema:
continue
schema_validator = self.schema_validator.evolve(schema)
if not schema_validator.format_validator(value):
continue
return str(schema.getkey("format"))
return None

def iter_valid_schemas(self, value: Any) -> Iterator[Spec]:
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/unmarshalling/test_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,25 @@ def test_nultiple_types_invalid(self, unmarshallers_factory, types, value):
assert len(exc_info.value.schema_errors) == 1
assert "is not of type" in exc_info.value.schema_errors[0].message

@pytest.mark.parametrize(
"types,format,value,expected",
[
(["string", "null"], "date", None, None),
(["string", "null"], "date", "2018-12-13", date(2018, 12, 13)),
],
)
def test_multiple_types_format_valid(self, unmarshallers_factory, types, format, value, expected):
schema = {
"type": types,
"format": format,
}
spec = Spec.from_dict(schema, validator=None)
unmarshaller = unmarshallers_factory.create(spec)

result = unmarshaller.unmarshal(value)

assert result == expected

def test_any_null(self, unmarshallers_factory):
schema = {}
spec = Spec.from_dict(schema, validator=None)
Expand Down

0 comments on commit 05da0e4

Please sign in to comment.