Skip to content

Commit

Permalink
More media types supported
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed Jul 19, 2023
1 parent efdc5be commit 38c4678
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
11 changes: 9 additions & 2 deletions openapi_core/deserializing/media_types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from json import loads
from json import loads as json_loads
from xml.etree.ElementTree import fromstring as xml_loads

from openapi_core.deserializing.media_types.datatypes import (
MediaTypeDeserializersDict,
Expand All @@ -7,12 +8,18 @@
MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.media_types.util import data_form_loads
from openapi_core.deserializing.media_types.util import plain_loads
from openapi_core.deserializing.media_types.util import urlencoded_form_loads

__all__ = ["media_type_deserializers_factory"]

media_type_deserializers: MediaTypeDeserializersDict = {
"application/json": loads,
"text/html": plain_loads,
"text/plain": plain_loads,
"application/json": json_loads,
"application/vnd.api+json": json_loads,
"application/xml": xml_loads,
"application/xhtml+xml": xml_loads,
"application/x-www-form-urlencoded": urlencoded_form_loads,
"multipart/form-data": data_form_loads,
}
Expand Down
6 changes: 6 additions & 0 deletions openapi_core/deserializing/media_types/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from urllib.parse import parse_qsl


def plain_loads(value: Union[str, bytes]) -> str:
if isinstance(value, bytes):
value = value.decode("ASCII", errors="surrogateescape")
return value


def urlencoded_form_loads(value: Any) -> Dict[str, Any]:
return dict(parse_qsl(value))

Expand Down
3 changes: 1 addition & 2 deletions tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ def test_get_pets_response_no_schema(self, spec):
data = "<html></html>"
response = MockResponse(data, status_code=404, mimetype="text/html")

with pytest.warns(UserWarning):
response_result = unmarshal_response(request, response, spec=spec)
response_result = unmarshal_response(request, response, spec=spec)

assert response_result.errors == []
assert response_result.data == data
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/unmarshalling/test_request_unmarshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,7 @@ def test_post_pets_plain_no_schema(self, request_unmarshaller):
mimetype="text/plain",
)

with pytest.warns(UserWarning):
result = request_unmarshaller.unmarshal(request)
result = request_unmarshaller.unmarshal(request)

assert result.errors == []
assert result.parameters == Parameters(
Expand Down

0 comments on commit 38c4678

Please sign in to comment.