diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index 39617f51..a8077308 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -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( diff --git a/tests/integration/unmarshalling/test_unmarshallers.py b/tests/integration/unmarshalling/test_unmarshallers.py index 6fa0708d..274fa732 100644 --- a/tests/integration/unmarshalling/test_unmarshallers.py +++ b/tests/integration/unmarshalling/test_unmarshallers.py @@ -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", [ @@ -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( @@ -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 = { @@ -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", @@ -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", diff --git a/tests/unit/templating/test_paths_finders.py b/tests/unit/templating/test_paths_finders.py index 17396d1a..e26e70c7 100644 --- a/tests/unit/templating/test_paths_finders.py +++ b/tests/unit/templating/test_paths_finders.py @@ -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" diff --git a/tests/unit/unmarshalling/test_schema_unmarshallers.py b/tests/unit/unmarshalling/test_schema_unmarshallers.py index c25e9005..9d005e99 100644 --- a/tests/unit/unmarshalling/test_schema_unmarshallers.py +++ b/tests/unit/unmarshalling/test_schema_unmarshallers.py @@ -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