Skip to content

Commit

Permalink
Formats raise error for other types fix
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Jun 22, 2023
1 parent bf862ad commit afa33f0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
3 changes: 3 additions & 0 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ def unmarshal(self, value: Any) -> Any:
schema_format = self.find_format(value)
if schema_format is None:
return typed
# ignore incompatible formats
if not isinstance(value, str):
return typed
return self.formats_unmarshaller.unmarshal(schema_format, typed)

def get_type_unmarshaller(
Expand Down
4 changes: 2 additions & 2 deletions openapi_core/unmarshalling/schemas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def format_uuid(value: Any) -> UUID:
return UUID(value)


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


def format_number(value: str) -> Union[int, float]:
Expand Down
28 changes: 13 additions & 15 deletions tests/integration/unmarshalling/test_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ def test_basic_type_formats_invalid(
@pytest.mark.parametrize(
"value,expected",
[
("dGVzdA==", "test"),
("dGVzdA==", b"test"),
("test", b"\xb5\xeb-"),
],
)
def test_string_byte(self, unmarshallers_factory, value, expected):
Expand Down Expand Up @@ -374,23 +375,17 @@ def test_string_uuid_invalid(self, unmarshallers_factory):
assert len(exc_info.value.schema_errors) == 1
assert f"is not a 'uuid'" in exc_info.value.schema_errors[0].message

@pytest.mark.xfail(
reason=(
"Formats raise error for other types. "
"See https://github.com/python-openapi/openapi-schema-validator/issues/66"
)
)
@pytest.mark.parametrize(
"type,format,value,expected",
[
("string", "float", "test", "test"),
("string", "double", "test", "test"),
("string", "byte", "test", "test"),
("integer", "date", "10", 10),
("integer", "date-time", "10", 10),
("integer", "byte", 10, 10),
("integer", "date", 10, 10),
("integer", "date-time", 10, 10),
("string", "int32", "test", "test"),
("string", "int64", "test", "test"),
("integer", "password", "10", 10),
("integer", "password", 10, 10),
],
)
def test_formats_ignored(
Expand Down Expand Up @@ -1679,7 +1674,7 @@ def test_not_nullable(self, unmarshallers_factory, type):
@pytest.mark.parametrize(
"type,format,value,unmarshalled",
[
("string", "byte", "dGVzdA==", "test"),
("string", "byte", "dGVzdA==", b"test"),
("string", "binary", b"test", b"test"),
],
)
Expand Down Expand Up @@ -1728,7 +1723,8 @@ def test_basic_type_oas30_formats_invalid(
reason=(
"OAS 3.0 string type checker allows byte. "
"See https://github.com/python-openapi/openapi-schema-validator/issues/64"
)
),
strict=True,
)
def test_string_format_binary_invalid(self, unmarshallers_factory):
schema = {
Expand All @@ -1748,7 +1744,8 @@ def test_string_format_binary_invalid(self, unmarshallers_factory):
reason=(
"Rraises TypeError not SchemaError. "
"See ttps://github.com/python-openapi/openapi-schema-validator/issues/65"
)
),
strict=True,
)
@pytest.mark.parametrize(
"types,value",
Expand Down Expand Up @@ -1928,7 +1925,8 @@ def unmarshallers_factory(self):
reason=(
"OpenAPI 3.1 schema validator uses OpenAPI 3.0 format checker."
"See https://github.com/python-openapi/openapi-core/issues/506"
)
),
strict=True,
)
@pytest.mark.parametrize(
"type,format",
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/templating/test_paths_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ class BaseTestServerNotFound:
def servers(self):
return []

@pytest.mark.xfail(reason="returns default server")
@pytest.mark.xfail(
reason="returns default server",
strict=True,
)
def test_raises(self, finder):
method = "get"
full_url = "http://petstore.swagger.io/resource"
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/unmarshalling/test_schema_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ def custom_format_validator(value):
reason=(
"Not registered format raises FormatterNotFoundError"
"See https://github.com/python-openapi/openapi-core/issues/515"
)
),
strict=True,
)
def test_schema_format_validator_format_invalid(
self, schema_unmarshaller_factory, unmarshaller_factory
Expand Down

0 comments on commit afa33f0

Please sign in to comment.