Skip to content

Ignore formats for other types in unmarshalling process #599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ 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)
or
# Workaround allows bytes for binary and byte formats
(isinstance(value, bytes) and schema_format in ["binary", "byte"])
):
return typed
return self.formats_unmarshaller.unmarshal(schema_format, typed)

def get_type_unmarshaller(
Expand Down
47 changes: 34 additions & 13 deletions tests/integration/unmarshalling/test_unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@ def test_basic_type_formats(

assert result == unmarshalled

@pytest.mark.parametrize(
"type,format,value",
[
("string", "float", "test"),
("string", "double", "test"),
("number", "date", 3),
("number", "date-time", 3),
("number", "uuid", 3),
],
)
def test_basic_type_formats_ignored(
self, unmarshallers_factory, type, format, value
):
schema = {
"type": type,
"format": format,
}
spec = Spec.from_dict(schema, validator=None)
unmarshaller = unmarshallers_factory.create(spec)

result = unmarshaller.unmarshal(value)

assert result == value

@pytest.mark.parametrize(
"type,format,value",
[
Expand Down Expand Up @@ -374,23 +398,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 @@ -1728,7 +1746,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 +1767,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 +1948,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
4 changes: 3 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,9 @@ class BaseTestServerNotFound:
def servers(self):
return []

@pytest.mark.xfail(reason="returns default server")
@pytest.mark.xfail(
reason="returns default server",
)
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