Skip to content

Commit

Permalink
Merge pull request #676 from python-openapi/fix/parameter-deserialize…
Browse files Browse the repository at this point in the history
…r-renamed-into-style-deserializer

Parameter deserializers renamed Style deserializers
  • Loading branch information
p1c2u authored Sep 22, 2023
2 parents cbbe084 + cf3d356 commit 0da2a38
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 88 deletions.
7 changes: 0 additions & 7 deletions openapi_core/deserializing/parameters/__init__.py

This file was deleted.

31 changes: 0 additions & 31 deletions openapi_core/deserializing/parameters/factories.py

This file was deleted.

7 changes: 7 additions & 0 deletions openapi_core/deserializing/styles/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from openapi_core.deserializing.styles.factories import (
StyleDeserializersFactory,
)

__all__ = ["style_deserializers_factory"]

style_deserializers_factory = StyleDeserializersFactory()
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
from typing import Optional

from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.parameters.datatypes import (
DeserializerCallable,
)
from openapi_core.deserializing.parameters.exceptions import (
from openapi_core.deserializing.styles.datatypes import DeserializerCallable
from openapi_core.deserializing.styles.exceptions import (
EmptyQueryParameterValue,
)
from openapi_core.schema.parameters import get_aslist
from openapi_core.schema.parameters import get_explode
from openapi_core.spec import Spec


class CallableParameterDeserializer:
class CallableStyleDeserializer:
def __init__(
self,
param_or_header: Spec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@


@dataclass
class BaseParameterDeserializeError(DeserializeError):
"""Base parameter deserialize operation error"""
class BaseStyleDeserializeError(DeserializeError):
"""Base style deserialize operation error"""

location: str


@dataclass
class ParameterDeserializeError(BaseParameterDeserializeError):
class ParameterDeserializeError(BaseStyleDeserializeError):
"""Parameter deserialize operation error"""

style: str
Expand All @@ -25,7 +25,7 @@ def __str__(self) -> str:


@dataclass(init=False)
class EmptyQueryParameterValue(BaseParameterDeserializeError):
class EmptyQueryParameterValue(BaseStyleDeserializeError):
name: str

def __init__(self, name: str):
Expand Down
29 changes: 29 additions & 0 deletions openapi_core/deserializing/styles/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import re
from functools import partial
from typing import Dict

from openapi_core.deserializing.styles.datatypes import DeserializerCallable
from openapi_core.deserializing.styles.deserializers import (
CallableStyleDeserializer,
)
from openapi_core.deserializing.styles.util import split
from openapi_core.schema.parameters import get_style
from openapi_core.spec import Spec


class StyleDeserializersFactory:
STYLE_DESERIALIZERS: Dict[str, DeserializerCallable] = {
"form": partial(split, separator=","),
"simple": partial(split, separator=","),
"spaceDelimited": partial(split, separator=" "),
"pipeDelimited": partial(split, separator="|"),
"deepObject": partial(re.split, pattern=r"\[|\]"),
}

def create(self, param_or_header: Spec) -> CallableStyleDeserializer:
style = get_style(param_or_header)

deserialize_callable = self.STYLE_DESERIALIZERS.get(style)
return CallableStyleDeserializer(
param_or_header, style, deserialize_callable
)
File renamed without changes.
14 changes: 6 additions & 8 deletions openapi_core/unmarshalling/request/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.parameters import (
parameter_deserializers_factory,
)
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
from openapi_core.deserializing.styles import style_deserializers_factory
from openapi_core.deserializing.styles.factories import (
StyleDeserializersFactory,
)
from openapi_core.protocols import BaseRequest
from openapi_core.protocols import Request
Expand Down Expand Up @@ -86,7 +84,7 @@ def __init__(
spec: Spec,
base_url: Optional[str] = None,
schema_casters_factory: SchemaCastersFactory = schema_casters_factory,
parameter_deserializers_factory: ParameterDeserializersFactory = parameter_deserializers_factory,
style_deserializers_factory: StyleDeserializersFactory = style_deserializers_factory,
media_type_deserializers_factory: MediaTypeDeserializersFactory = media_type_deserializers_factory,
schema_validators_factory: Optional[SchemaValidatorsFactory] = None,
format_validators: Optional[FormatValidatorsDict] = None,
Expand All @@ -106,7 +104,7 @@ def __init__(
spec,
base_url=base_url,
schema_casters_factory=schema_casters_factory,
parameter_deserializers_factory=parameter_deserializers_factory,
style_deserializers_factory=style_deserializers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
schema_validators_factory=schema_validators_factory,
format_validators=format_validators,
Expand All @@ -121,7 +119,7 @@ def __init__(
spec,
base_url=base_url,
schema_casters_factory=schema_casters_factory,
parameter_deserializers_factory=parameter_deserializers_factory,
style_deserializers_factory=style_deserializers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
schema_validators_factory=schema_validators_factory,
format_validators=format_validators,
Expand Down
12 changes: 5 additions & 7 deletions openapi_core/unmarshalling/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.parameters import (
parameter_deserializers_factory,
)
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
from openapi_core.deserializing.styles import style_deserializers_factory
from openapi_core.deserializing.styles.factories import (
StyleDeserializersFactory,
)
from openapi_core.spec import Spec
from openapi_core.unmarshalling.schemas.datatypes import (
Expand All @@ -40,7 +38,7 @@ def __init__(
spec: Spec,
base_url: Optional[str] = None,
schema_casters_factory: SchemaCastersFactory = schema_casters_factory,
parameter_deserializers_factory: ParameterDeserializersFactory = parameter_deserializers_factory,
style_deserializers_factory: StyleDeserializersFactory = style_deserializers_factory,
media_type_deserializers_factory: MediaTypeDeserializersFactory = media_type_deserializers_factory,
schema_validators_factory: Optional[SchemaValidatorsFactory] = None,
format_validators: Optional[FormatValidatorsDict] = None,
Expand All @@ -62,7 +60,7 @@ def __init__(
spec,
base_url=base_url,
schema_casters_factory=schema_casters_factory,
parameter_deserializers_factory=parameter_deserializers_factory,
style_deserializers_factory=style_deserializers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
schema_validators_factory=schema_validators_factory,
format_validators=format_validators,
Expand Down
12 changes: 5 additions & 7 deletions openapi_core/validation/request/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.parameters import (
parameter_deserializers_factory,
)
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
from openapi_core.deserializing.styles import style_deserializers_factory
from openapi_core.deserializing.styles.factories import (
StyleDeserializersFactory,
)
from openapi_core.protocols import BaseRequest
from openapi_core.protocols import Request
Expand Down Expand Up @@ -68,7 +66,7 @@ def __init__(
spec: Spec,
base_url: Optional[str] = None,
schema_casters_factory: SchemaCastersFactory = schema_casters_factory,
parameter_deserializers_factory: ParameterDeserializersFactory = parameter_deserializers_factory,
style_deserializers_factory: StyleDeserializersFactory = style_deserializers_factory,
media_type_deserializers_factory: MediaTypeDeserializersFactory = media_type_deserializers_factory,
schema_validators_factory: Optional[SchemaValidatorsFactory] = None,
format_validators: Optional[FormatValidatorsDict] = None,
Expand All @@ -82,7 +80,7 @@ def __init__(
spec,
base_url=base_url,
schema_casters_factory=schema_casters_factory,
parameter_deserializers_factory=parameter_deserializers_factory,
style_deserializers_factory=style_deserializers_factory,
media_type_deserializers_factory=media_type_deserializers_factory,
schema_validators_factory=schema_validators_factory,
format_validators=format_validators,
Expand Down
26 changes: 13 additions & 13 deletions openapi_core/validation/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
from openapi_core.deserializing.media_types.factories import (
MediaTypeDeserializersFactory,
)
from openapi_core.deserializing.parameters import (
parameter_deserializers_factory,
)
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
from openapi_core.deserializing.styles import style_deserializers_factory
from openapi_core.deserializing.styles.factories import (
StyleDeserializersFactory,
)
from openapi_core.protocols import Request
from openapi_core.protocols import WebhookRequest
Expand All @@ -44,7 +42,7 @@ def __init__(
spec: Spec,
base_url: Optional[str] = None,
schema_casters_factory: SchemaCastersFactory = schema_casters_factory,
parameter_deserializers_factory: ParameterDeserializersFactory = parameter_deserializers_factory,
style_deserializers_factory: StyleDeserializersFactory = style_deserializers_factory,
media_type_deserializers_factory: MediaTypeDeserializersFactory = media_type_deserializers_factory,
schema_validators_factory: Optional[SchemaValidatorsFactory] = None,
format_validators: Optional[FormatValidatorsDict] = None,
Expand All @@ -57,7 +55,7 @@ def __init__(
self.base_url = base_url

self.schema_casters_factory = schema_casters_factory
self.parameter_deserializers_factory = parameter_deserializers_factory
self.style_deserializers_factory = style_deserializers_factory
self.media_type_deserializers_factory = (
media_type_deserializers_factory
)
Expand All @@ -78,15 +76,15 @@ def _get_media_type(self, content: Spec, mimetype: str) -> MediaType:
finder = MediaTypeFinder(content)
return finder.find(mimetype)

def _deserialise_data(self, mimetype: str, value: Any) -> Any:
def _deserialise_media_type(self, mimetype: str, value: Any) -> Any:
deserializer = self.media_type_deserializers_factory.create(
mimetype,
extra_media_type_deserializers=self.extra_media_type_deserializers,
)
return deserializer.deserialize(value)

def _deserialise_parameter(self, param: Spec, value: Any) -> Any:
deserializer = self.parameter_deserializers_factory.create(param)
def _deserialise_style(self, param_or_header: Spec, value: Any) -> Any:
deserializer = self.style_deserializers_factory.create(param_or_header)
return deserializer.deserialize(value)

def _cast(self, schema: Spec, value: Any) -> Any:
Expand Down Expand Up @@ -144,15 +142,17 @@ def _get_param_or_header_value_and_schema(
else:
# Simple scenario
if "content" not in param_or_header:
deserialised = self._deserialise_parameter(
deserialised = self._deserialise_style(
param_or_header, raw_value
)
schema = param_or_header / "schema"
# Complex scenario
else:
content = param_or_header / "content"
mimetype, media_type = next(content.items())
deserialised = self._deserialise_data(mimetype, raw_value)
deserialised = self._deserialise_media_type(
mimetype, raw_value
)
schema = media_type / "schema"
casted = self._cast(schema, deserialised)
return casted, schema
Expand All @@ -161,7 +161,7 @@ def _get_content_value_and_schema(
self, raw: Any, mimetype: str, content: Spec
) -> Tuple[Any, Optional[Spec]]:
media_type, mimetype = self._get_media_type(content, mimetype)
deserialised = self._deserialise_data(mimetype, raw)
deserialised = self._deserialise_media_type(mimetype, raw)
casted = self._cast(media_type, deserialised)

if "schema" not in media_type:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_petstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from openapi_core.casting.schemas.exceptions import CastError
from openapi_core.datatypes import Parameters
from openapi_core.deserializing.exceptions import DeserializeError
from openapi_core.deserializing.parameters.exceptions import (
from openapi_core.deserializing.styles.exceptions import (
EmptyQueryParameterValue,
)
from openapi_core.templating.media_types.exceptions import MediaTypeNotFound
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import pytest

from openapi_core.deserializing.parameters.exceptions import (
from openapi_core.deserializing.styles.exceptions import (
EmptyQueryParameterValue,
)
from openapi_core.deserializing.parameters.factories import (
ParameterDeserializersFactory,
from openapi_core.deserializing.styles.factories import (
StyleDeserializersFactory,
)
from openapi_core.spec.paths import Spec


class TestParameterDeserializer:
class TestStyleDeserializer:
@pytest.fixture
def deserializer_factory(self):
def create_deserializer(param):
return ParameterDeserializersFactory().create(param)
return StyleDeserializersFactory().create(param)

return create_deserializer

Expand Down

0 comments on commit 0da2a38

Please sign in to comment.