Skip to content

Commit

Permalink
refactor: update body() to not decode bytes
Browse files Browse the repository at this point in the history
The underling openapi-code library changed how they pass back the
response body in 0.19

Refs: python-openapi/openapi-core#710

Co-authored-by: Wim De Clercq <declewi@gmail.com>
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman and Wim-De-Clercq committed Mar 7, 2024
1 parent 8686e3b commit f615095
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
3 changes: 0 additions & 3 deletions pyramid_openapi3/tests/test_contenttypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ def test_post_json(self) -> None:
res = self._testapp().post_json("/foo", {"bar": "baz"}, status=200)
self.assertEqual(res.json, {"bar": "zab"})

# FIXME: Something deep in request validation isn't happy right now.
# Might be related: https://github.com/Pylons/pyramid_openapi3/issues/199
@unittest.expectedFailure
def test_post_form(self) -> None: # pragma: no cover
"""Post with `application/x-www-form-urlencoded`."""

Expand Down
4 changes: 2 additions & 2 deletions pyramid_openapi3/tests/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_relative_app_request() -> None:
assert openapi_request.path == "/subpath/foo"
assert openapi_request.path_pattern == "/foo"
assert openapi_request.method == "get"
assert openapi_request.body == ""
assert openapi_request.body == b""
assert openapi_request.mimetype == "text/html"
assert openapi_request.content_type == "text/html"

Expand Down Expand Up @@ -114,7 +114,7 @@ def test_mapped_values_response() -> None:

openapi_response = PyramidOpenAPIResponse(pyramid_request.response)

assert openapi_response.data == ""
assert openapi_response.data == b""
assert openapi_response.status_code == 200
assert openapi_response.mimetype == "text/html"
assert openapi_response.content_type == "text/html"
Expand Down
12 changes: 5 additions & 7 deletions pyramid_openapi3/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@ def method(self) -> str:
return self.request.method.lower()

@property
def body(self) -> t.Optional[t.Union[str, t.Dict]]:
"""The request body, as string.""" # noqa D401
def body(self) -> t.Optional[t.Union[bytes, str, t.Dict]]:
"""The request body.""" # noqa D401
if "multipart/form-data" == self.request.content_type:
return self.request.POST.mixed()
if isinstance(self.request.body, bytes):
return self.request.body.decode("utf-8")
return self.request.body

@property
Expand All @@ -71,9 +69,9 @@ def __init__(self, response: Response) -> None:
self.response = response

@property
def data(self) -> str:
"""The response body, as string.""" # noqa D401
return self.response.text
def data(self) -> bytes:
"""The response body.""" # noqa D401
return self.response.body

@property
def status_code(self) -> int:
Expand Down

0 comments on commit f615095

Please sign in to comment.