Closed
Description
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 commentedon Dec 12, 2022
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
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.
additionalProperties without schema should allow null.
additionalProperties without schema should allow null.
additionalProperties without schema should allow null.
p1c2u commentedon Dec 17, 2022
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 commentedon Apr 17, 2023
@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 commentedon Apr 18, 2023
Yes it was merged hence closing