Skip to content

Add NullUnmarshaller #432

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 2 commits into from
Oct 10, 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
2 changes: 2 additions & 0 deletions openapi_core/unmarshalling/schemas/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from openapi_core.unmarshalling.schemas.unmarshallers import (
IntegerUnmarshaller,
)
from openapi_core.unmarshalling.schemas.unmarshallers import NullUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import NumberUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import ObjectUnmarshaller
from openapi_core.unmarshalling.schemas.unmarshallers import StringUnmarshaller
Expand All @@ -45,6 +46,7 @@ class SchemaUnmarshallersFactory:
"boolean": BooleanUnmarshaller,
"array": ArrayUnmarshaller,
"object": ObjectUnmarshaller,
"null": NullUnmarshaller,
"any": AnyUnmarshaller,
}

Expand Down
9 changes: 8 additions & 1 deletion openapi_core/unmarshalling/schemas/unmarshallers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from functools import partial
from typing import TYPE_CHECKING
from typing import Any
from typing import Dict
from typing import Iterable
from typing import List
from typing import Optional
Expand All @@ -12,6 +11,7 @@
from jsonschema._types import is_array
from jsonschema._types import is_bool
from jsonschema._types import is_integer
from jsonschema._types import is_null
from jsonschema._types import is_number
from jsonschema._types import is_object
from jsonschema.protocols import Validator
Expand Down Expand Up @@ -159,6 +159,13 @@ class BooleanUnmarshaller(BaseSchemaUnmarshaller):
}


class NullUnmarshaller(BaseSchemaUnmarshaller):

FORMATTERS: FormattersDict = {
None: Formatter.from_callables(partial(is_null, None), None),
}


class ComplexUnmarshaller(BaseSchemaUnmarshaller):
def __init__(
self,
Expand Down
42 changes: 37 additions & 5 deletions tests/unit/unmarshalling/test_unmarshal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import datetime
import uuid
from functools import partial

import pytest
from isodate.tzinfo import UTC
from isodate.tzinfo import FixedOffset
from openapi_schema_validator import OAS30Validator
from openapi_schema_validator import OAS31Validator

from openapi_core.spec.paths import Spec
from openapi_core.unmarshalling.schemas.enums import UnmarshalContext
Expand All @@ -23,21 +25,25 @@


@pytest.fixture
def unmarshaller_factory():
def schema_unmarshaller_factory():
def create_unmarshaller(
schema, custom_formatters=None, context=UnmarshalContext.REQUEST
validator, schema, custom_formatters=None, context=None
):
custom_formatters = custom_formatters or {}
return SchemaUnmarshallersFactory(
OAS30Validator,
validator,
custom_formatters=custom_formatters,
context=context,
).create(schema)

return create_unmarshaller


class TestUnmarshal:
class TestOAS30SchemaUnmarshallerUnmarshal:
@pytest.fixture
def unmarshaller_factory(self, schema_unmarshaller_factory):
return partial(schema_unmarshaller_factory, OAS30Validator)

def test_no_schema(self, unmarshaller_factory):
spec = None
value = "test"
Expand Down Expand Up @@ -79,7 +85,11 @@ def unmarshal(self, value):
).unmarshal(value)


class TestSchemaUnmarshallerCall:
class TestOAS30SchemaUnmarshallerCall:
@pytest.fixture
def unmarshaller_factory(self, schema_unmarshaller_factory):
return partial(schema_unmarshaller_factory, OAS30Validator)

def test_deprecated(self, unmarshaller_factory):
schema = {
"type": "string",
Expand Down Expand Up @@ -824,3 +834,25 @@ def test_additional_properties_list(self, unmarshaller_factory):
assert result == {
"user_ids": [1, 2, 3, 4],
}


class TestOAS31SchemaUnmarshallerCall:
@pytest.fixture
def unmarshaller_factory(self, schema_unmarshaller_factory):
return partial(schema_unmarshaller_factory, OAS31Validator)

def test_null(self, unmarshaller_factory):
schema = {"type": "null"}
spec = Spec.from_dict(schema)

result = unmarshaller_factory(spec)(None)

assert result is None

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

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