From cf3d3567e5209f251acc6071a28f098465d6c4c1 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Fri, 22 Sep 2023 09:04:32 +0000 Subject: [PATCH] Parameter deesrialized renamed Style deserializer --- .../deserializing/parameters/__init__.py | 7 ----- .../deserializing/parameters/factories.py | 31 ------------------- openapi_core/deserializing/styles/__init__.py | 7 +++++ .../{parameters => styles}/datatypes.py | 0 .../{parameters => styles}/deserializers.py | 8 ++--- .../{parameters => styles}/exceptions.py | 8 ++--- .../deserializing/styles/factories.py | 29 +++++++++++++++++ .../{parameters => styles}/util.py | 0 .../unmarshalling/request/unmarshallers.py | 14 ++++----- openapi_core/unmarshalling/unmarshallers.py | 12 +++---- openapi_core/validation/request/validators.py | 12 +++---- openapi_core/validation/validators.py | 26 ++++++++-------- tests/integration/test_petstore.py | 2 +- ...lizers.py => test_styles_deserializers.py} | 10 +++--- 14 files changed, 78 insertions(+), 88 deletions(-) delete mode 100644 openapi_core/deserializing/parameters/__init__.py delete mode 100644 openapi_core/deserializing/parameters/factories.py create mode 100644 openapi_core/deserializing/styles/__init__.py rename openapi_core/deserializing/{parameters => styles}/datatypes.py (100%) rename openapi_core/deserializing/{parameters => styles}/deserializers.py (90%) rename openapi_core/deserializing/{parameters => styles}/exceptions.py (74%) create mode 100644 openapi_core/deserializing/styles/factories.py rename openapi_core/deserializing/{parameters => styles}/util.py (100%) rename tests/unit/deserializing/{test_parameters_deserializers.py => test_styles_deserializers.py} (83%) diff --git a/openapi_core/deserializing/parameters/__init__.py b/openapi_core/deserializing/parameters/__init__.py deleted file mode 100644 index 6859c906..00000000 --- a/openapi_core/deserializing/parameters/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -from openapi_core.deserializing.parameters.factories import ( - ParameterDeserializersFactory, -) - -__all__ = ["parameter_deserializers_factory"] - -parameter_deserializers_factory = ParameterDeserializersFactory() diff --git a/openapi_core/deserializing/parameters/factories.py b/openapi_core/deserializing/parameters/factories.py deleted file mode 100644 index e0f559d2..00000000 --- a/openapi_core/deserializing/parameters/factories.py +++ /dev/null @@ -1,31 +0,0 @@ -import re -from functools import partial -from typing import Dict - -from openapi_core.deserializing.parameters.datatypes import ( - DeserializerCallable, -) -from openapi_core.deserializing.parameters.deserializers import ( - CallableParameterDeserializer, -) -from openapi_core.deserializing.parameters.util import split -from openapi_core.schema.parameters import get_style -from openapi_core.spec import Spec - - -class ParameterDeserializersFactory: - PARAMETER_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) -> CallableParameterDeserializer: - style = get_style(param_or_header) - - deserialize_callable = self.PARAMETER_STYLE_DESERIALIZERS.get(style) - return CallableParameterDeserializer( - param_or_header, style, deserialize_callable - ) diff --git a/openapi_core/deserializing/styles/__init__.py b/openapi_core/deserializing/styles/__init__.py new file mode 100644 index 00000000..b5191289 --- /dev/null +++ b/openapi_core/deserializing/styles/__init__.py @@ -0,0 +1,7 @@ +from openapi_core.deserializing.styles.factories import ( + StyleDeserializersFactory, +) + +__all__ = ["style_deserializers_factory"] + +style_deserializers_factory = StyleDeserializersFactory() diff --git a/openapi_core/deserializing/parameters/datatypes.py b/openapi_core/deserializing/styles/datatypes.py similarity index 100% rename from openapi_core/deserializing/parameters/datatypes.py rename to openapi_core/deserializing/styles/datatypes.py diff --git a/openapi_core/deserializing/parameters/deserializers.py b/openapi_core/deserializing/styles/deserializers.py similarity index 90% rename from openapi_core/deserializing/parameters/deserializers.py rename to openapi_core/deserializing/styles/deserializers.py index ae93b718..16b92d1a 100644 --- a/openapi_core/deserializing/parameters/deserializers.py +++ b/openapi_core/deserializing/styles/deserializers.py @@ -5,10 +5,8 @@ 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 @@ -16,7 +14,7 @@ from openapi_core.spec import Spec -class CallableParameterDeserializer: +class CallableStyleDeserializer: def __init__( self, param_or_header: Spec, diff --git a/openapi_core/deserializing/parameters/exceptions.py b/openapi_core/deserializing/styles/exceptions.py similarity index 74% rename from openapi_core/deserializing/parameters/exceptions.py rename to openapi_core/deserializing/styles/exceptions.py index 146d60a1..e423843f 100644 --- a/openapi_core/deserializing/parameters/exceptions.py +++ b/openapi_core/deserializing/styles/exceptions.py @@ -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 @@ -25,7 +25,7 @@ def __str__(self) -> str: @dataclass(init=False) -class EmptyQueryParameterValue(BaseParameterDeserializeError): +class EmptyQueryParameterValue(BaseStyleDeserializeError): name: str def __init__(self, name: str): diff --git a/openapi_core/deserializing/styles/factories.py b/openapi_core/deserializing/styles/factories.py new file mode 100644 index 00000000..a7a711e0 --- /dev/null +++ b/openapi_core/deserializing/styles/factories.py @@ -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 + ) diff --git a/openapi_core/deserializing/parameters/util.py b/openapi_core/deserializing/styles/util.py similarity index 100% rename from openapi_core/deserializing/parameters/util.py rename to openapi_core/deserializing/styles/util.py diff --git a/openapi_core/unmarshalling/request/unmarshallers.py b/openapi_core/unmarshalling/request/unmarshallers.py index ac2bbf99..3a201176 100644 --- a/openapi_core/unmarshalling/request/unmarshallers.py +++ b/openapi_core/unmarshalling/request/unmarshallers.py @@ -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 @@ -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, @@ -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, @@ -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, diff --git a/openapi_core/unmarshalling/unmarshallers.py b/openapi_core/unmarshalling/unmarshallers.py index af857906..5efaf5bf 100644 --- a/openapi_core/unmarshalling/unmarshallers.py +++ b/openapi_core/unmarshalling/unmarshallers.py @@ -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 ( @@ -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, @@ -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, diff --git a/openapi_core/validation/request/validators.py b/openapi_core/validation/request/validators.py index d0bf3609..fc21a933 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -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 @@ -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, @@ -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, diff --git a/openapi_core/validation/validators.py b/openapi_core/validation/validators.py index d2858de9..4fbd7e36 100644 --- a/openapi_core/validation/validators.py +++ b/openapi_core/validation/validators.py @@ -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 @@ -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, @@ -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 ) @@ -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: @@ -144,7 +142,7 @@ 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" @@ -152,7 +150,9 @@ def _get_param_or_header_value_and_schema( 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 @@ -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: diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index 6a7055d1..2d8794d5 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -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 diff --git a/tests/unit/deserializing/test_parameters_deserializers.py b/tests/unit/deserializing/test_styles_deserializers.py similarity index 83% rename from tests/unit/deserializing/test_parameters_deserializers.py rename to tests/unit/deserializing/test_styles_deserializers.py index 2247dea4..9d4d7094 100644 --- a/tests/unit/deserializing/test_parameters_deserializers.py +++ b/tests/unit/deserializing/test_styles_deserializers.py @@ -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