Skip to content

Commit a04da78

Browse files
committed
Formats raise error for other types fix
1 parent bf862ad commit a04da78

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

Diff for: openapi_core/unmarshalling/schemas/unmarshallers.py

+8
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ def unmarshal(self, value: Any) -> Any:
273273
schema_format = self.find_format(value)
274274
if schema_format is None:
275275
return typed
276+
# ignore incompatible formats
277+
if not (
278+
isinstance(value, str)
279+
or
280+
# Workaround allows bytes for binary and byte formats
281+
(isinstance(value, bytes) and schema_format in ["binary", "byte"])
282+
):
283+
return typed
276284
return self.formats_unmarshaller.unmarshal(schema_format, typed)
277285

278286
def get_type_unmarshaller(

Diff for: tests/integration/unmarshalling/test_unmarshallers.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,30 @@ def test_basic_type_formats(
240240

241241
assert result == unmarshalled
242242

243+
@pytest.mark.parametrize(
244+
"type,format,value",
245+
[
246+
("string", "float", "test"),
247+
("string", "double", "test"),
248+
("number", "date", 3),
249+
("number", "date-time", 3),
250+
("number", "uuid", 3),
251+
],
252+
)
253+
def test_basic_type_formats_ignored(
254+
self, unmarshallers_factory, type, format, value
255+
):
256+
schema = {
257+
"type": type,
258+
"format": format,
259+
}
260+
spec = Spec.from_dict(schema, validator=None)
261+
unmarshaller = unmarshallers_factory.create(spec)
262+
263+
result = unmarshaller.unmarshal(value)
264+
265+
assert result == value
266+
243267
@pytest.mark.parametrize(
244268
"type,format,value",
245269
[
@@ -374,23 +398,17 @@ def test_string_uuid_invalid(self, unmarshallers_factory):
374398
assert len(exc_info.value.schema_errors) == 1
375399
assert f"is not a 'uuid'" in exc_info.value.schema_errors[0].message
376400

377-
@pytest.mark.xfail(
378-
reason=(
379-
"Formats raise error for other types. "
380-
"See https://github.com./python-openapi/openapi-schema-validator/issues/66"
381-
)
382-
)
383401
@pytest.mark.parametrize(
384402
"type,format,value,expected",
385403
[
386404
("string", "float", "test", "test"),
387405
("string", "double", "test", "test"),
388-
("string", "byte", "test", "test"),
389-
("integer", "date", "10", 10),
390-
("integer", "date-time", "10", 10),
406+
("integer", "byte", 10, 10),
407+
("integer", "date", 10, 10),
408+
("integer", "date-time", 10, 10),
391409
("string", "int32", "test", "test"),
392410
("string", "int64", "test", "test"),
393-
("integer", "password", "10", 10),
411+
("integer", "password", 10, 10),
394412
],
395413
)
396414
def test_formats_ignored(
@@ -1728,7 +1746,8 @@ def test_basic_type_oas30_formats_invalid(
17281746
reason=(
17291747
"OAS 3.0 string type checker allows byte. "
17301748
"See https://github.com./python-openapi/openapi-schema-validator/issues/64"
1731-
)
1749+
),
1750+
strict=True,
17321751
)
17331752
def test_string_format_binary_invalid(self, unmarshallers_factory):
17341753
schema = {
@@ -1748,7 +1767,8 @@ def test_string_format_binary_invalid(self, unmarshallers_factory):
17481767
reason=(
17491768
"Rraises TypeError not SchemaError. "
17501769
"See ttps://github.com./python-openapi/openapi-schema-validator/issues/65"
1751-
)
1770+
),
1771+
strict=True,
17521772
)
17531773
@pytest.mark.parametrize(
17541774
"types,value",
@@ -1928,7 +1948,8 @@ def unmarshallers_factory(self):
19281948
reason=(
19291949
"OpenAPI 3.1 schema validator uses OpenAPI 3.0 format checker."
19301950
"See https://github.com./python-openapi/openapi-core/issues/506"
1931-
)
1951+
),
1952+
strict=True,
19321953
)
19331954
@pytest.mark.parametrize(
19341955
"type,format",

Diff for: tests/unit/templating/test_paths_finders.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ class BaseTestServerNotFound:
173173
def servers(self):
174174
return []
175175

176-
@pytest.mark.xfail(reason="returns default server")
176+
@pytest.mark.xfail(
177+
reason="returns default server",
178+
)
177179
def test_raises(self, finder):
178180
method = "get"
179181
full_url = "http://petstore.swagger.io/resource"

Diff for: tests/unit/unmarshalling/test_schema_unmarshallers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def custom_format_validator(value):
197197
reason=(
198198
"Not registered format raises FormatterNotFoundError"
199199
"See https://github.com./python-openapi/openapi-core/issues/515"
200-
)
200+
),
201+
strict=True,
201202
)
202203
def test_schema_format_validator_format_invalid(
203204
self, schema_unmarshaller_factory, unmarshaller_factory

0 commit comments

Comments
 (0)