From e8c681bb5c133dc89cbecb79b415d9e06604f5f0 Mon Sep 17 00:00:00 2001 From: p1c2u Date: Fri, 20 Jan 2023 11:41:26 +0000 Subject: [PATCH] Rename UnmarshalContext to ValidationContext --- docs/customizations.rst | 2 +- .../unmarshalling/schemas/__init__.py | 6 +++--- openapi_core/unmarshalling/schemas/enums.py | 2 +- .../unmarshalling/schemas/factories.py | 10 +++++----- .../unmarshalling/schemas/unmarshallers.py | 8 ++++---- tests/unit/unmarshalling/test_unmarshal.py | 20 +++++++++---------- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/docs/customizations.rst b/docs/customizations.rst index b8c43e81..6d77de2e 100644 --- a/docs/customizations.rst +++ b/docs/customizations.rst @@ -72,7 +72,7 @@ Here's how you could add support for a ``usdate`` format that handles dates of t schema_unmarshallers_factory = SchemaUnmarshallersFactory( OAS30Validator, custom_formatters=custom_formatters, - context=UnmarshalContext.RESPONSE, + context=ValidationContext.RESPONSE, ) result = validate_response( diff --git a/openapi_core/unmarshalling/schemas/__init__.py b/openapi_core/unmarshalling/schemas/__init__.py index 0a3e2cf1..47b40055 100644 --- a/openapi_core/unmarshalling/schemas/__init__.py +++ b/openapi_core/unmarshalling/schemas/__init__.py @@ -1,7 +1,7 @@ from openapi_schema_validator import OAS30Validator from openapi_schema_validator import OAS31Validator -from openapi_core.unmarshalling.schemas.enums import UnmarshalContext +from openapi_core.unmarshalling.schemas.enums import ValidationContext from openapi_core.unmarshalling.schemas.factories import ( SchemaUnmarshallersFactory, ) @@ -16,12 +16,12 @@ oas30_request_schema_unmarshallers_factory = SchemaUnmarshallersFactory( OAS30Validator, - context=UnmarshalContext.REQUEST, + context=ValidationContext.REQUEST, ) oas30_response_schema_unmarshallers_factory = SchemaUnmarshallersFactory( OAS30Validator, - context=UnmarshalContext.RESPONSE, + context=ValidationContext.RESPONSE, ) oas31_schema_unmarshallers_factory = SchemaUnmarshallersFactory( diff --git a/openapi_core/unmarshalling/schemas/enums.py b/openapi_core/unmarshalling/schemas/enums.py index cfe332e8..2f8d88f2 100644 --- a/openapi_core/unmarshalling/schemas/enums.py +++ b/openapi_core/unmarshalling/schemas/enums.py @@ -2,6 +2,6 @@ from enum import Enum -class UnmarshalContext(Enum): +class ValidationContext(Enum): REQUEST = "request" RESPONSE = "response" diff --git a/openapi_core/unmarshalling/schemas/factories.py b/openapi_core/unmarshalling/schemas/factories.py index 08ef5c39..5bec2d37 100644 --- a/openapi_core/unmarshalling/schemas/factories.py +++ b/openapi_core/unmarshalling/schemas/factories.py @@ -17,7 +17,7 @@ from openapi_core.spec import Spec from openapi_core.unmarshalling.schemas.datatypes import CustomFormattersDict from openapi_core.unmarshalling.schemas.datatypes import FormattersDict -from openapi_core.unmarshalling.schemas.enums import UnmarshalContext +from openapi_core.unmarshalling.schemas.enums import ValidationContext from openapi_core.unmarshalling.schemas.exceptions import ( FormatterNotFoundError, ) @@ -49,15 +49,15 @@ class SchemaValidatorsFactory: CONTEXTS = { - UnmarshalContext.REQUEST: "write", - UnmarshalContext.RESPONSE: "read", + ValidationContext.REQUEST: "write", + ValidationContext.RESPONSE: "read", } def __init__( self, schema_validator_class: Type[Validator], custom_formatters: Optional[CustomFormattersDict] = None, - context: Optional[UnmarshalContext] = None, + context: Optional[ValidationContext] = None, ): self.schema_validator_class = schema_validator_class if custom_formatters is None: @@ -105,7 +105,7 @@ def __init__( self, schema_validator_class: Type[Validator], custom_formatters: Optional[CustomFormattersDict] = None, - context: Optional[UnmarshalContext] = None, + context: Optional[ValidationContext] = None, ): self.schema_validator_class = schema_validator_class if custom_formatters is None: diff --git a/openapi_core/unmarshalling/schemas/unmarshallers.py b/openapi_core/unmarshalling/schemas/unmarshallers.py index 28e1db88..d064a1ff 100644 --- a/openapi_core/unmarshalling/schemas/unmarshallers.py +++ b/openapi_core/unmarshalling/schemas/unmarshallers.py @@ -24,7 +24,7 @@ from openapi_core.schema.schemas import get_properties from openapi_core.spec import Spec from openapi_core.unmarshalling.schemas.datatypes import FormattersDict -from openapi_core.unmarshalling.schemas.enums import UnmarshalContext +from openapi_core.unmarshalling.schemas.enums import ValidationContext from openapi_core.unmarshalling.schemas.exceptions import ( FormatterNotFoundError, ) @@ -273,7 +273,7 @@ def __init__( formatter: Optional[Formatter], validators_factory: "SchemaValidatorsFactory", unmarshallers_factory: "SchemaUnmarshallersFactory", - context: Optional[UnmarshalContext] = None, + context: Optional[ValidationContext] = None, ): super().__init__( schema, @@ -360,10 +360,10 @@ def _unmarshal_properties( for prop_name, prop_schema in get_properties(self.schema).items(): read_only = prop_schema.getkey("readOnly", False) - if self.context == UnmarshalContext.REQUEST and read_only: + if self.context == ValidationContext.REQUEST and read_only: continue write_only = prop_schema.getkey("writeOnly", False) - if self.context == UnmarshalContext.RESPONSE and write_only: + if self.context == ValidationContext.RESPONSE and write_only: continue try: prop_value = value[prop_name] diff --git a/tests/unit/unmarshalling/test_unmarshal.py b/tests/unit/unmarshalling/test_unmarshal.py index 507f2bce..4991e98e 100644 --- a/tests/unit/unmarshalling/test_unmarshal.py +++ b/tests/unit/unmarshalling/test_unmarshal.py @@ -9,7 +9,7 @@ from openapi_schema_validator import OAS31Validator from openapi_core.spec.paths import Spec -from openapi_core.unmarshalling.schemas.enums import UnmarshalContext +from openapi_core.unmarshalling.schemas.enums import ValidationContext from openapi_core.unmarshalling.schemas.exceptions import ( FormatterNotFoundError, ) @@ -866,9 +866,9 @@ def test_read_only_properties(self, unmarshaller_factory): spec = Spec.from_dict(schema, validator=None) # readOnly properties may be admitted in a Response context - result = unmarshaller_factory(spec, context=UnmarshalContext.RESPONSE)( - {"id": 10} - ) + result = unmarshaller_factory( + spec, context=ValidationContext.RESPONSE + )({"id": 10}) assert result == { "id": 10, @@ -889,7 +889,7 @@ def test_read_only_properties_invalid(self, unmarshaller_factory): # readOnly properties are not admitted on a Request context with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(spec, context=UnmarshalContext.REQUEST)( + unmarshaller_factory(spec, context=ValidationContext.REQUEST)( {"id": 10} ) @@ -907,7 +907,7 @@ def test_write_only_properties(self, unmarshaller_factory): spec = Spec.from_dict(schema, validator=None) # readOnly properties may be admitted in a Response context - result = unmarshaller_factory(spec, context=UnmarshalContext.REQUEST)( + result = unmarshaller_factory(spec, context=ValidationContext.REQUEST)( {"id": 10} ) @@ -930,7 +930,7 @@ def test_write_only_properties_invalid(self, unmarshaller_factory): # readOnly properties are not admitted on a Request context with pytest.raises(InvalidSchemaValue): - unmarshaller_factory(spec, context=UnmarshalContext.RESPONSE)( + unmarshaller_factory(spec, context=ValidationContext.RESPONSE)( {"id": 10} ) @@ -938,9 +938,9 @@ def test_additional_properties_list(self, unmarshaller_factory): schema = {"type": "object"} spec = Spec.from_dict(schema, validator=None) - result = unmarshaller_factory(spec, context=UnmarshalContext.RESPONSE)( - {"user_ids": [1, 2, 3, 4]} - ) + result = unmarshaller_factory( + spec, context=ValidationContext.RESPONSE + )({"user_ids": [1, 2, 3, 4]}) assert result == { "user_ids": [1, 2, 3, 4],