Skip to content

Commit afa33f0

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

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

openapi_core/unmarshalling/schemas/unmarshallers.py

+3
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ 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 isinstance(value, str):
278+
return typed
276279
return self.formats_unmarshaller.unmarshal(schema_format, typed)
277280

278281
def get_type_unmarshaller(

openapi_core/unmarshalling/schemas/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def format_uuid(value: Any) -> UUID:
1717
return UUID(value)
1818

1919

20-
def format_byte(value: str, encoding: str = "utf8") -> str:
21-
return str(b64decode(value), encoding)
20+
def format_byte(value: str) -> bytes:
21+
return b64decode(value)
2222

2323

2424
def format_number(value: str) -> Union[int, float]:

tests/integration/unmarshalling/test_unmarshallers.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ def test_basic_type_formats_invalid(
268268
@pytest.mark.parametrize(
269269
"value,expected",
270270
[
271-
("dGVzdA==", "test"),
271+
("dGVzdA==", b"test"),
272+
("test", b"\xb5\xeb-"),
272273
],
273274
)
274275
def test_string_byte(self, unmarshallers_factory, value, expected):
@@ -374,23 +375,17 @@ def test_string_uuid_invalid(self, unmarshallers_factory):
374375
assert len(exc_info.value.schema_errors) == 1
375376
assert f"is not a 'uuid'" in exc_info.value.schema_errors[0].message
376377

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-
)
383378
@pytest.mark.parametrize(
384379
"type,format,value,expected",
385380
[
386381
("string", "float", "test", "test"),
387382
("string", "double", "test", "test"),
388-
("string", "byte", "test", "test"),
389-
("integer", "date", "10", 10),
390-
("integer", "date-time", "10", 10),
383+
("integer", "byte", 10, 10),
384+
("integer", "date", 10, 10),
385+
("integer", "date-time", 10, 10),
391386
("string", "int32", "test", "test"),
392387
("string", "int64", "test", "test"),
393-
("integer", "password", "10", 10),
388+
("integer", "password", 10, 10),
394389
],
395390
)
396391
def test_formats_ignored(
@@ -1679,7 +1674,7 @@ def test_not_nullable(self, unmarshallers_factory, type):
16791674
@pytest.mark.parametrize(
16801675
"type,format,value,unmarshalled",
16811676
[
1682-
("string", "byte", "dGVzdA==", "test"),
1677+
("string", "byte", "dGVzdA==", b"test"),
16831678
("string", "binary", b"test", b"test"),
16841679
],
16851680
)
@@ -1728,7 +1723,8 @@ def test_basic_type_oas30_formats_invalid(
17281723
reason=(
17291724
"OAS 3.0 string type checker allows byte. "
17301725
"See https://github.com/python-openapi/openapi-schema-validator/issues/64"
1731-
)
1726+
),
1727+
strict=True,
17321728
)
17331729
def test_string_format_binary_invalid(self, unmarshallers_factory):
17341730
schema = {
@@ -1748,7 +1744,8 @@ def test_string_format_binary_invalid(self, unmarshallers_factory):
17481744
reason=(
17491745
"Rraises TypeError not SchemaError. "
17501746
"See ttps://github.com/python-openapi/openapi-schema-validator/issues/65"
1751-
)
1747+
),
1748+
strict=True,
17521749
)
17531750
@pytest.mark.parametrize(
17541751
"types,value",
@@ -1928,7 +1925,8 @@ def unmarshallers_factory(self):
19281925
reason=(
19291926
"OpenAPI 3.1 schema validator uses OpenAPI 3.0 format checker."
19301927
"See https://github.com/python-openapi/openapi-core/issues/506"
1931-
)
1928+
),
1929+
strict=True,
19321930
)
19331931
@pytest.mark.parametrize(
19341932
"type,format",

tests/unit/templating/test_paths_finders.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ 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+
strict=True,
179+
)
177180
def test_raises(self, finder):
178181
method = "get"
179182
full_url = "http://petstore.swagger.io/resource"

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)