From fbb0bcd9335a5a6f31e660c30537522693cb90c3 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Tue, 31 Oct 2023 15:14:17 +0000 Subject: [PATCH] Form urlencoded primitive properies default decode --- .../media_types/deserializers.py | 5 +- tests/integration/test_petstore.py | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/openapi_core/deserializing/media_types/deserializers.py b/openapi_core/deserializing/media_types/deserializers.py index 532417ec..e7169c4c 100644 --- a/openapi_core/deserializing/media_types/deserializers.py +++ b/openapi_core/deserializing/media_types/deserializers.py @@ -126,10 +126,7 @@ def decode_property( ) -> Any: if self.encoding is None or prop_name not in self.encoding: prop_schema_type = prop_schema.getkey("type", "") - if ( - self.mimetype == "application/x-www-form-urlencoded" - and prop_schema_type in ["array", "object"] - ): + if self.mimetype == "application/x-www-form-urlencoded": # default serialization strategy for complex objects # in the application/x-www-form-urlencoded return self.decode_property_style( diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index 20569b2a..2fa5441f 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -1778,6 +1778,80 @@ def test_post_tags_created_datetime(self, spec): assert response_result.data.rootCause == rootCause assert response_result.data.additionalinfo == additionalinfo + def test_post_tags_urlencoded(self, spec): + host_url = "http://petstore.swagger.io/v1" + path_pattern = "/v1/tags" + created = "2016-04-16T16:06:05Z" + pet_name = "Dog" + data_json = { + "created": created, + "name": pet_name, + } + data = urlencode(data_json) + content_type = "application/x-www-form-urlencoded" + + request = MockRequest( + host_url, + "POST", + "/tags", + path_pattern=path_pattern, + data=data, + content_type=content_type, + ) + + result = unmarshal_request( + request, + spec=spec, + cls=V30RequestParametersUnmarshaller, + ) + + assert result.parameters == Parameters() + + result = unmarshal_request( + request, spec=spec, cls=V30RequestBodyUnmarshaller + ) + + assert is_dataclass(result.body) + assert result.body.created == datetime( + 2016, 4, 16, 16, 6, 5, tzinfo=UTC + ) + assert result.body.name == pet_name + + code = 400 + message = "Bad request" + rootCause = "Tag already exist" + additionalinfo = "Tag Dog already exist" + response_data_json = { + "code": code, + "message": message, + "rootCause": rootCause, + "additionalinfo": additionalinfo, + } + response_data = json.dumps(response_data_json) + response = MockResponse(response_data, status_code=404) + + result = unmarshal_response( + request, + response, + spec=spec, + cls=V30ResponseDataUnmarshaller, + ) + + assert is_dataclass(result.data) + assert result.data.code == code + assert result.data.message == message + assert result.data.rootCause == rootCause + assert result.data.additionalinfo == additionalinfo + + response_result = unmarshal_response(request, response, spec=spec) + + assert response_result.errors == [] + assert is_dataclass(response_result.data) + assert response_result.data.code == code + assert response_result.data.message == message + assert response_result.data.rootCause == rootCause + assert response_result.data.additionalinfo == additionalinfo + def test_post_tags_created_invalid_type(self, spec): host_url = "http://petstore.swagger.io/v1" path_pattern = "/v1/tags"