From 9a348dcb4c70eeebd478746157b45b1d19bb3910 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 ------ openapi_core/deserializing/styles/__init__.py | 7 ++++++ .../{parameters => styles}/datatypes.py | 0 .../{parameters => styles}/deserializers.py | 6 ++--- .../{parameters => styles}/exceptions.py | 8 +++---- .../{parameters => styles}/factories.py | 18 +++++++------- .../{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 | 24 +++++++++---------- tests/integration/test_petstore.py | 2 +- ...lizers.py => test_styles_deserializers.py} | 10 ++++---- 13 files changed, 60 insertions(+), 60 deletions(-) delete mode 100644 openapi_core/deserializing/parameters/__init__.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 (91%) rename openapi_core/deserializing/{parameters => styles}/exceptions.py (74%) rename openapi_core/deserializing/{parameters => styles}/factories.py (51%) 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/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 91% rename from openapi_core/deserializing/parameters/deserializers.py rename to openapi_core/deserializing/styles/deserializers.py index ae93b718..d94b5fde 100644 --- a/openapi_core/deserializing/parameters/deserializers.py +++ b/openapi_core/deserializing/styles/deserializers.py @@ -5,10 +5,10 @@ from typing import Optional from openapi_core.deserializing.exceptions import DeserializeError -from openapi_core.deserializing.parameters.datatypes import ( +from openapi_core.deserializing.styles.datatypes import ( DeserializerCallable, ) -from openapi_core.deserializing.parameters.exceptions import ( +from openapi_core.deserializing.styles.exceptions import ( EmptyQueryParameterValue, ) from openapi_core.schema.parameters import get_aslist @@ -16,7 +16,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/parameters/factories.py b/openapi_core/deserializing/styles/factories.py similarity index 51% rename from openapi_core/deserializing/parameters/factories.py rename to openapi_core/deserializing/styles/factories.py index e0f559d2..f0f34974 100644 --- a/openapi_core/deserializing/parameters/factories.py +++ b/openapi_core/deserializing/styles/factories.py @@ -2,19 +2,19 @@ from functools import partial from typing import Dict -from openapi_core.deserializing.parameters.datatypes import ( +from openapi_core.deserializing.styles.datatypes import ( DeserializerCallable, ) -from openapi_core.deserializing.parameters.deserializers import ( - CallableParameterDeserializer, +from openapi_core.deserializing.styles.deserializers import ( + CallableStyleDeserializer, ) -from openapi_core.deserializing.parameters.util import split +from openapi_core.deserializing.styles.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] = { +class StyleDeserializersFactory: + STYLE_DESERIALIZERS: Dict[str, DeserializerCallable] = { "form": partial(split, separator=","), "simple": partial(split, separator=","), "spaceDelimited": partial(split, separator=" "), @@ -22,10 +22,10 @@ class ParameterDeserializersFactory: "deepObject": partial(re.split, pattern=r"\[|\]"), } - def create(self, param_or_header: Spec) -> CallableParameterDeserializer: + def create(self, param_or_header: Spec) -> CallableStyleDeserializer: style = get_style(param_or_header) - deserialize_callable = self.PARAMETER_STYLE_DESERIALIZERS.get(style) - return CallableParameterDeserializer( + 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..c4ea27db 100644 --- a/openapi_core/unmarshalling/request/unmarshallers.py +++ b/openapi_core/unmarshalling/request/unmarshallers.py @@ -11,11 +11,11 @@ from openapi_core.deserializing.media_types.factories import ( MediaTypeDeserializersFactory, ) -from openapi_core.deserializing.parameters import ( - parameter_deserializers_factory, +from openapi_core.deserializing.styles import ( + style_deserializers_factory, ) -from openapi_core.deserializing.parameters.factories import ( - ParameterDeserializersFactory, +from openapi_core.deserializing.styles.factories import ( + StyleDeserializersFactory, ) from openapi_core.protocols import BaseRequest from openapi_core.protocols import Request @@ -86,7 +86,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 +106,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 +121,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..ff1740c1 100644 --- a/openapi_core/unmarshalling/unmarshallers.py +++ b/openapi_core/unmarshalling/unmarshallers.py @@ -14,11 +14,11 @@ from openapi_core.deserializing.media_types.factories import ( MediaTypeDeserializersFactory, ) -from openapi_core.deserializing.parameters import ( - parameter_deserializers_factory, +from openapi_core.deserializing.styles import ( + style_deserializers_factory, ) -from openapi_core.deserializing.parameters.factories import ( - ParameterDeserializersFactory, +from openapi_core.deserializing.styles.factories import ( + StyleDeserializersFactory, ) from openapi_core.spec import Spec from openapi_core.unmarshalling.schemas.datatypes import ( @@ -40,7 +40,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 +62,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..17235532 100644 --- a/openapi_core/validation/request/validators.py +++ b/openapi_core/validation/request/validators.py @@ -18,11 +18,11 @@ from openapi_core.deserializing.media_types.factories import ( MediaTypeDeserializersFactory, ) -from openapi_core.deserializing.parameters import ( - parameter_deserializers_factory, +from openapi_core.deserializing.styles import ( + style_deserializers_factory, ) -from openapi_core.deserializing.parameters.factories import ( - ParameterDeserializersFactory, +from openapi_core.deserializing.styles.factories import ( + StyleDeserializersFactory, ) from openapi_core.protocols import BaseRequest from openapi_core.protocols import Request @@ -68,7 +68,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 +82,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..e124af35 100644 --- a/openapi_core/validation/validators.py +++ b/openapi_core/validation/validators.py @@ -17,11 +17,11 @@ from openapi_core.deserializing.media_types.factories import ( MediaTypeDeserializersFactory, ) -from openapi_core.deserializing.parameters import ( - parameter_deserializers_factory, +from openapi_core.deserializing.styles import ( + style_deserializers_factory, ) -from openapi_core.deserializing.parameters.factories import ( - ParameterDeserializersFactory, +from openapi_core.deserializing.styles.factories import ( + StyleDeserializersFactory, ) from openapi_core.protocols import Request from openapi_core.protocols import WebhookRequest @@ -44,7 +44,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 +57,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 +78,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 +144,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 +152,7 @@ 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