Merge pull request #302 from p1c2u/feature/media-type-deserialize-form-urlencoded

media type deserialize form urlencoded
This commit is contained in:
A 2021-03-24 20:40:40 +00:00 committed by GitHub
commit 7a83f019c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 113 additions and 89 deletions

View file

@ -1,4 +1,4 @@
from openapi_core.deserializing.media_types.util import json_loads
from openapi_core.deserializing.media_types.util import json_loads, form_loads
from openapi_core.deserializing.media_types.deserializers import (
PrimitiveDeserializer,
@ -9,6 +9,7 @@ class MediaTypeDeserializersFactory(object):
MEDIA_TYPE_DESERIALIZERS = {
'application/json': json_loads,
'application/x-www-form-urlencoded': form_loads,
}
def __init__(self, custom_deserializers=None):

View file

@ -1,6 +1,7 @@
from json import loads
from six import binary_type
from six.moves.urllib.parse import parse_qsl
def json_loads(value):
@ -8,3 +9,7 @@ def json_loads(value):
if isinstance(value, (binary_type, )):
value = value.decode()
return loads(value)
def form_loads(value):
return dict(parse_qsl(value))

View file

@ -1,88 +0,0 @@
import pytest
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
)
from openapi_core.deserializing.parameters.exceptions import (
EmptyParameterValue,
)
from openapi_core.schema.media_types.models import MediaType
from openapi_core.schema.parameters.models import Parameter
class TestParameterDeserialise(object):
@pytest.fixture
def deserializer_factory(self):
def create_deserializer(param):
return ParameterDeserializersFactory().create(param)
return create_deserializer
def test_deprecated(self, deserializer_factory):
param = Parameter('param', 'query', deprecated=True)
value = 'test'
with pytest.warns(DeprecationWarning):
result = deserializer_factory(param)(value)
assert result == value
def test_query_empty(self, deserializer_factory):
param = Parameter('param', 'query')
value = ''
with pytest.raises(EmptyParameterValue):
deserializer_factory(param)(value)
def test_query_valid(self, deserializer_factory):
param = Parameter('param', 'query')
value = 'test'
result = deserializer_factory(param)(value)
assert result == value
class TestMediaTypeDeserialise(object):
@pytest.fixture
def deserializer_factory(self):
def create_deserializer(media_type, custom_deserializers=None):
return MediaTypeDeserializersFactory(
custom_deserializers=custom_deserializers).create(media_type)
return create_deserializer
def test_empty(self, deserializer_factory):
media_type = MediaType('application/json')
value = ''
with pytest.raises(DeserializeError):
deserializer_factory(media_type)(value)
def test_no_schema_deserialised(self, deserializer_factory):
media_type = MediaType('application/json')
value = "{}"
result = deserializer_factory(media_type)(value)
assert result == {}
def test_no_schema_custom_deserialiser(self, deserializer_factory):
custom_mimetype = 'application/custom'
media_type = MediaType(custom_mimetype)
value = "{}"
def custom_deserializer(value):
return 'custom'
custom_deserializers = {
custom_mimetype: custom_deserializer,
}
result = deserializer_factory(
media_type, custom_deserializers=custom_deserializers)(value)
assert result == 'custom'

View file

@ -0,0 +1,64 @@
import pytest
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)
from openapi_core.schema.media_types.models import MediaType
class TestMediaTypeDeserializer(object):
@pytest.fixture
def deserializer_factory(self):
def create_deserializer(media_type, custom_deserializers=None):
return MediaTypeDeserializersFactory(
custom_deserializers=custom_deserializers).create(media_type)
return create_deserializer
def test_json_empty(self, deserializer_factory):
media_type = MediaType('application/json')
value = ''
with pytest.raises(DeserializeError):
deserializer_factory(media_type)(value)
def test_json_empty_object(self, deserializer_factory):
media_type = MediaType('application/json')
value = "{}"
result = deserializer_factory(media_type)(value)
assert result == {}
def test_form_urlencoded_empty(self, deserializer_factory):
media_type = MediaType('application/x-www-form-urlencoded')
value = ''
result = deserializer_factory(media_type)(value)
assert result == {}
def test_form_urlencoded_simple(self, deserializer_factory):
media_type = MediaType('application/x-www-form-urlencoded')
value = 'param1=test'
result = deserializer_factory(media_type)(value)
assert result == {'param1': 'test'}
def test_custom_simple(self, deserializer_factory):
custom_mimetype = 'application/custom'
media_type = MediaType(custom_mimetype)
value = "{}"
def custom_deserializer(value):
return 'custom'
custom_deserializers = {
custom_mimetype: custom_deserializer,
}
result = deserializer_factory(
media_type, custom_deserializers=custom_deserializers)(value)
assert result == 'custom'

View file

@ -0,0 +1,42 @@
import pytest
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
)
from openapi_core.deserializing.parameters.exceptions import (
EmptyParameterValue,
)
from openapi_core.schema.parameters.models import Parameter
class TestParameterDeserializer(object):
@pytest.fixture
def deserializer_factory(self):
def create_deserializer(param):
return ParameterDeserializersFactory().create(param)
return create_deserializer
def test_deprecated(self, deserializer_factory):
param = Parameter('param', 'query', deprecated=True)
value = 'test'
with pytest.warns(DeprecationWarning):
result = deserializer_factory(param)(value)
assert result == value
def test_query_empty(self, deserializer_factory):
param = Parameter('param', 'query')
value = ''
with pytest.raises(EmptyParameterValue):
deserializer_factory(param)(value)
def test_query_valid(self, deserializer_factory):
param = Parameter('param', 'query')
value = 'test'
result = deserializer_factory(param)(value)
assert result == value