Skip to content

[0.16.2 regression] {"key": null} fails to validate as {"type": "object"} #442

Closed
@andersk

Description

@andersk
Contributor

In 0.16.1, {"key": null} validated successfully against {"type": "object"}—an object that includes a null value is still an object. But in 0.16.2 this incorrectly raises openapi_core.unmarshalling.schemas.exceptions.InvalidSchemaValue: Value None not valid for schema of type any: (<ValidationError: 'None for not nullable'>,).

This regression was introduced by commit 692a915 (#434).

Full reproducible example:

from openapi_core import Spec
from openapi_core.testing import MockRequest
from openapi_core.validation.request import openapi_request_validator

spec = Spec.create(
    {
        "openapi": "3.0.3",
        "info": {"title": "test", "version": "0"},
        "paths": {
            "/test": {
                "post": {
                    "parameters": [
                        {
                            "name": "obj",
                            "in": "query",
                            "content": {
                                "application/json": {
                                    "schema": {"type": "object"},
                                }
                            },
                        },
                    ],
                    "responses": {"200": {"description": ""}},
                },
            },
        },
    },
)
request = MockRequest(
    "http://localhost/", "post", "/test", args={"obj": '{"key": null}'}
)
result = openapi_request_validator.validate(spec, request)
print(result)
result.raise_for_errors()

Activity

Wim-De-Clercq

Wim-De-Clercq commented on Dec 12, 2022

@Wim-De-Clercq
Contributor

The openapi-schema-validator update introduced this https://github.com/p1c2u/openapi-schema-validator/blob/553d606ad0289e81564b7c60aed05fd0e745f6bd/openapi_schema_validator/validators.py#L62
Which makes every schema have a default "nullable: false" requirement. Which I believe is correct. You should specify "nullable: true" if you want to have nullable properties.

The problem is that openapi-core creates a little dummy schema for the "additionalProperties" -- for any key not present in the schema -- like so:
https://github.com/p1c2u/openapi-core/blob/4055a365591d9ef5ac8c93a5ebaee61c19f04fe0/openapi_core/unmarshalling/schemas/unmarshallers.py#L295

But now that little schema will have a "nullable: false" check, and it incorrectly raises exceptions for this.

The above line should be updated to

additional_prop_schema = Spec.from_dict({"nullable": True})

Or perhaps a little refactor is in place to not create any schema and just take the value as is. It feels a bit wasteful to constantly create these little schemas and do null-validation checks for something which isn't even defined in the schema in the first place.

p1c2u

p1c2u commented on Dec 17, 2022

@p1c2u
Collaborator

Or perhaps a little refactor is in place to not create any schema and just take the value as is. It feels a bit wasteful to constantly create these little schemas and do null-validation checks for something which isn't even defined in the schema in the first place.

I must admit it was silly. I will change that .

EDIT: Now I remember why I made this change. It was to unmarshall additional properties to objects instead of just returning pure python dict.

Wim-De-Clercq

Wim-De-Clercq commented on Apr 17, 2023

@Wim-De-Clercq
Contributor

@p1c2u
Do you reckon this bug is actually fixed in the meantime? If I git-checkout 0.17.1 and run the above test (with updated imports to reflect changes) it seems to report no errors.

Update: ah yes, I see my commit got merged: e017634
Fixed since >= 0.16.3

I guess we can close this issue (and related still open PRs). I'll leave it to you.

p1c2u

p1c2u commented on Apr 18, 2023

@p1c2u
Collaborator

Yes it was merged hence closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @andersk@p1c2u@Wim-De-Clercq

      Issue actions

        [0.16.2 regression] `{"key": null}` fails to validate as `{"type": "object"}` · Issue #442 · python-openapi/openapi-core