Skip to content

Commit 05da0e4

Browse files
committed
multi types schema format unmarshal fix
1 parent b91e977 commit 05da0e4

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

openapi_core/unmarshalling/schemas/unmarshallers.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,10 @@ def unmarshal(self, schema_format: str, value: Any) -> Any:
228228
return value
229229
try:
230230
return format_unmarshaller(value)
231-
except (ValueError, TypeError) as exc:
232-
raise FormatUnmarshalError(value, schema_format, exc)
231+
except (ValueError, TypeError):
232+
return value
233233

234-
def get_unmarshaller(
235-
self, schema_format: str
236-
) -> Optional[FormatUnmarshaller]:
234+
def get_unmarshaller(self, schema_format: str) -> Optional[FormatUnmarshaller]:
237235
if schema_format in self.custom_formatters:
238236
formatter = self.custom_formatters[schema_format]
239237
return formatter.format
@@ -316,8 +314,12 @@ def evolve(self, schema: Spec) -> "SchemaUnmarshaller":
316314

317315
def find_format(self, value: Any) -> Optional[str]:
318316
for schema in self.iter_valid_schemas(value):
319-
if "format" in schema:
320-
return str(schema.getkey("format"))
317+
if "format" not in schema:
318+
continue
319+
schema_validator = self.schema_validator.evolve(schema)
320+
if not schema_validator.format_validator(value):
321+
continue
322+
return str(schema.getkey("format"))
321323
return None
322324

323325
def iter_valid_schemas(self, value: Any) -> Iterator[Spec]:

tests/integration/unmarshalling/test_unmarshallers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,25 @@ def test_nultiple_types_invalid(self, unmarshallers_factory, types, value):
20602060
assert len(exc_info.value.schema_errors) == 1
20612061
assert "is not of type" in exc_info.value.schema_errors[0].message
20622062

2063+
@pytest.mark.parametrize(
2064+
"types,format,value,expected",
2065+
[
2066+
(["string", "null"], "date", None, None),
2067+
(["string", "null"], "date", "2018-12-13", date(2018, 12, 13)),
2068+
],
2069+
)
2070+
def test_multiple_types_format_valid(self, unmarshallers_factory, types, format, value, expected):
2071+
schema = {
2072+
"type": types,
2073+
"format": format,
2074+
}
2075+
spec = Spec.from_dict(schema, validator=None)
2076+
unmarshaller = unmarshallers_factory.create(spec)
2077+
2078+
result = unmarshaller.unmarshal(value)
2079+
2080+
assert result == expected
2081+
20632082
def test_any_null(self, unmarshallers_factory):
20642083
schema = {}
20652084
spec = Spec.from_dict(schema, validator=None)

0 commit comments

Comments
 (0)