Skip to content

Multi type unmarshaller #433

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2022
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
10 changes: 10 additions & 0 deletions openapi_core/unmarshalling/schemas/factories.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings
from typing import Any
from typing import Dict
from typing import Iterable
from typing import Optional
from typing import Type
from typing import Union
Expand Down Expand Up @@ -30,6 +31,9 @@
from openapi_core.unmarshalling.schemas.unmarshallers import (
IntegerUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import (
MultiTypeUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import NullUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import NumberUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import ObjectUnmarshaller
Expand Down Expand Up @@ -89,6 +93,12 @@ def create(
formatter = self.custom_formatters.get(schema_format)

schema_type = type_override or schema.getkey("type", "any")
if isinstance(schema_type, Iterable) and not isinstance(
schema_type, str
):
return MultiTypeUnmarshaller(
schema, validator, formatter, self, context=self.context
)
if schema_type in self.COMPLEX_UNMARSHALLERS:
complex_klass = self.COMPLEX_UNMARSHALLERS[schema_type]
return complex_klass(
Expand Down
21 changes: 21 additions & 0 deletions openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,27 @@ def _unmarshal_object(self, value: Any) -> Any:
return properties


class MultiTypeUnmarshaller(ComplexUnmarshaller):
@property
def types_unmarshallers(self) -> List["BaseSchemaUnmarshaller"]:
types = self.schema.getkey("type", ["any"])
unmarshaller = partial(self.unmarshallers_factory.create, self.schema)
return list(map(unmarshaller, types))

def unmarshal(self, value: Any) -> Any:
for unmarshaller in self.types_unmarshallers:
# validate with validator of formatter (usualy type validator)
try:
unmarshaller._formatter_validate(value)
except ValidateError:
continue
else:
return unmarshaller(value)

log.warning("failed to unmarshal multi type")
return value


class AnyUnmarshaller(ComplexUnmarshaller):

SCHEMA_TYPES_ORDER = [
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/unmarshalling/test_unmarshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,34 @@ def test_additional_properties_list(self, unmarshaller_factory):
"user_ids": [1, 2, 3, 4],
}

@pytest.mark.xfail(message="None and NOTSET should be distinguished")
def test_null_not_supported(self, unmarshaller_factory):
schema = {"type": "null"}
spec = Spec.from_dict(schema)

with pytest.raises(InvalidSchemaValue):
unmarshaller_factory(spec)(None)

@pytest.mark.parametrize(
"types,value",
[
(["string", "null"], "string"),
(["number", "null"], 2),
(["number", "null"], 3.14),
(["boolean", "null"], True),
(["array", "null"], [1, 2]),
(["object", "null"], {}),
],
)
def test_nultiple_types_not_supported(
self, unmarshaller_factory, types, value
):
schema = {"type": types}
spec = Spec.from_dict(schema)

with pytest.raises(TypeError):
unmarshaller_factory(spec)(value)


class TestOAS31SchemaUnmarshallerCall:
@pytest.fixture
Expand All @@ -856,3 +884,40 @@ def test_null_invalid(self, unmarshaller_factory, value):

with pytest.raises(InvalidSchemaValue):
unmarshaller_factory(spec)(value)

@pytest.mark.parametrize(
"types,value",
[
(["string", "null"], "string"),
(["number", "null"], 2),
(["number", "null"], 3.14),
(["boolean", "null"], True),
(["array", "null"], [1, 2]),
(["object", "null"], {}),
],
)
def test_nultiple_types(self, unmarshaller_factory, types, value):
schema = {"type": types}
spec = Spec.from_dict(schema)

result = unmarshaller_factory(spec)(value)

assert result == value

@pytest.mark.parametrize(
"types,value",
[
(["string", "null"], 2),
(["number", "null"], "string"),
(["number", "null"], True),
(["boolean", "null"], 3.14),
(["array", "null"], {}),
(["object", "null"], [1, 2]),
],
)
def test_nultiple_types_invalid(self, unmarshaller_factory, types, value):
schema = {"type": types}
spec = Spec.from_dict(schema)

with pytest.raises(InvalidSchemaValue):
unmarshaller_factory(spec)(value)