Skip to content

Commit

Permalink
Merge pull request #709 from python-openapi/fix/form-urlencoded-primi…
Browse files Browse the repository at this point in the history
…tive-properties-default-decode-type-fix

Form urlencoded primitive properties default decode
  • Loading branch information
p1c2u committed Oct 31, 2023
2 parents 24e16e1 + fbb0bcd commit 78710c9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 4 deletions.
5 changes: 1 addition & 4 deletions openapi_core/deserializing/media_types/deserializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
74 changes: 74 additions & 0 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 78710c9

Please sign in to comment.