Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rename UnmarshalContext to ValidationContext #472

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/customizations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions openapi_core/unmarshalling/schemas/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion openapi_core/unmarshalling/schemas/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from enum import Enum


class UnmarshalContext(Enum):
class ValidationContext(Enum):
REQUEST = "request"
RESPONSE = "response"
10 changes: 5 additions & 5 deletions openapi_core/unmarshalling/schemas/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/unmarshalling/test_unmarshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
Expand All @@ -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}
)

Expand All @@ -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}
)

Expand All @@ -930,17 +930,17 @@ 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}
)

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],
Expand Down