Skip to content

[Bug]: Validation shouldn't fail when multiple types for a property include "null" #551

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

Closed
andres-mariano opened this issue Apr 10, 2023 · 1 comment · Fixed by #555
Closed
Labels

Comments

@andres-mariano
Copy link

Actual Behavior

An exception is raised when passing a null value even though the spec says it's a valid type.

openapi_core.unmarshalling.schemas.exceptions.UnmarshallerError: Unmarshaller not found for type(s)

Traceback:

Traceback (most recent call last):
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/validation/decorators.py", line 31, in wrapper
    return f(*args, **kwds)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/validation/request/validators.py", line 253, in _get_body
    return self._get_content_value(raw_body, mimetype, content)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/unmarshallers.py", line 113, in _get_content_value
    return self._unmarshal_schema(schema, casted)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/unmarshallers.py", line 90, in _unmarshal_schema
    return unmarshaller.unmarshal(value)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 299, in unmarshal
    typed = type_unmarshaller(value)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 57, in __call__
    properties = self._unmarshal_properties(value)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 111, in _unmarshal_properties
    properties[prop_name] = self.schema_unmarshaller.evolve(
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 299, in unmarshal
    typed = type_unmarshaller(value)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 145, in __call__
    unmarshaller = self._get_best_unmarshaller(value)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 169, in _get_best_unmarshaller
    raise UnmarshallerError("Unmarshaller not found for type(s)")
openapi_core.unmarshalling.schemas.exceptions.UnmarshallerError: Unmarshaller not found for type(s)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/user1/example_bug.py", line 41, in <module>
    unmarshal_request(request, spec)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/shortcuts.py", line 173, in unmarshal_request
    return unmarshal_apicall_request(
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/shortcuts.py", line 117, in unmarshal_apicall_request
    result.raise_for_errors()
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/datatypes.py", line 14, in raise_for_errors
    raise error
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/unmarshalling/request/unmarshallers.py", line 154, in _unmarshal
    body = self._get_body(request.body, request.mimetype, operation)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/validation/decorators.py", line 35, in wrapper
    self._raise_error(exc, self.err_cls, f, *args, **kwds)
  File "/Users/user1/venv/lib/python3.8/site-packages/openapi_core/validation/decorators.py", line 58, in _raise_error
    raise init(**kw) from exc
openapi_core.validation.request.exceptions.RequestBodyValidationError: Request body validation error

Expected Behavior

Should not raise an exception.

Steps to Reproduce

I believe the issue is that

class AnyUnmarshaller(MultiTypeUnmarshaller):
    SCHEMA_TYPES_ORDER = [
        "object",
        "array",
        "boolean",
        "integer",
        "number",
        "string",
    ]

in openapi_core/unmarshalling/schemas/unmarshallers.py doesn't contain the "null" type.

Minimal example to reproduce the issue:

from openapi_core import Spec
from openapi_core.testing import MockRequest
from openapi_core import unmarshal_request
spec = Spec.from_dict(
    {
        "openapi": "3.1.0",
        "info": {"title": "test", "version": "0"},
        "paths": {
            "/test": {
                "post": {
                    "requestBody": {
                        "required": True,
                        "content": {
                            "application/json": {
                                "schema": {
                                    "type": "object",
                                    "properties": {
                                        "name": {
                                            "oneOf": [
                                                {"type": "string"},
                                                {"type": "null"}
                                            ],
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "responses": {
                        "200": {
                            "description": "Success"
                        }
                    }
                }
            },
        },
    }
)
request = MockRequest(
    host_url="http://localhost/",
    method="post",
    path="/test",
    data=json.dumps({"name": None})
)
unmarshal_request(request, spec)

OpenAPI Core Version

0.17.1

OpenAPI Core Integration

none

Affected Area(s)

unmarshalling

References

No response

Anything else we need to know?

Adding "null" to the array mentioned above seems to fix the issue. I can add a PR for that but I'm not sure what's the protocol to do that.

Thanks!

Would you like to implement a fix?

Yes

@andres-mariano andres-mariano added the kind/bug Indicates an issue label Apr 10, 2023
@andres-mariano
Copy link
Author

Possible fix:

class AnyUnmarshaller(MultiTypeUnmarshaller):
    SCHEMA_TYPES_ORDER = [
        "object",
        "array",
        "boolean",
        "integer",
        "number",
        "string",
        "null",
    ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants