[Bug]: Can’t validate `integer`/`number`/`boolean` from `application/x-www-form-urlencoded` request body
andersk opened this issue · 4 comments
Actual Behavior
Traceback (most recent call last):
File "/home/anders/python/openapi-core/openapi_core/validation/decorators.py", line 31, in wrapper
return f(*args, **kwds)
File "/home/anders/python/openapi-core/openapi_core/validation/request/validators.py", line 260, in _get_body
value, _ = self._get_content_and_schema(raw_body, content, mimetype)
File "/home/anders/python/openapi-core/openapi_core/validation/validators.py", line 249, in _get_content_and_schema
self._validate_schema(schema, casted)
File "/home/anders/python/openapi-core/openapi_core/validation/validators.py", line 144, in _validate_schema
validator.validate(value)
File "/home/anders/python/openapi-core/openapi_core/validation/schemas/validators.py", line 36, in validate
raise InvalidSchemaValue(value, schema_type, schema_errors=errors)
openapi_core.validation.schemas.exceptions.InvalidSchemaValue: Value {'foo': '123'} not valid for schema of type object: (<ValidationError: "'123' is not of type 'integer'">,)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/anders/python/openapi-core/test.py", line 35, in <module>
validate_request(request, spec=spec)
File "/home/anders/python/openapi-core/openapi_core/shortcuts.py", line 321, in validate_request
validate_apicall_request(
File "/home/anders/python/openapi-core/openapi_core/shortcuts.py", line 396, in validate_apicall_request
return v.validate(request)
File "/home/anders/python/openapi-core/openapi_core/validation/request/validators.py", line 279, in validate
raise err
File "/home/anders/python/openapi-core/openapi_core/validation/request/validators.py", line 114, in _iter_errors
self._get_body(request.body, request.mimetype, operation)
File "/home/anders/python/openapi-core/openapi_core/validation/decorators.py", line 33, in wrapper
self._raise_error(exc, self.err_validate_cls, f, *args, **kwds)
File "/home/anders/python/openapi-core/openapi_core/validation/decorators.py", line 58, in _raise_error
raise init(**kw) from exc
openapi_core.validation.request.exceptions.InvalidRequestBody: Request body validation errorExpected Behavior
No error.
Steps to Reproduce
from openapi_core import Spec, validate_request
from openapi_core.testing import MockRequest
spec = Spec.from_dict(
{
"openapi": "3.1.0",
"info": {"version": "0", "title": "test"},
"paths": {
"/test": {
"post": {
"requestBody": {
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {"foo": {"type": "integer"}},
},
}
}
},
"responses": {"200": {"description": "OK"}},
}
}
},
}
)
request = MockRequest(
"http://localhost",
"post",
"/test",
data="foo=123",
mimetype="application/x-www-form-urlencoded",
)
validate_request(request, spec=spec)OpenAPI Core Version
0.18.1 or current Git (cc95ed0)
OpenAPI Core Integration
none
Affected Area(s)
No response
References
No response
Anything else we need to know?
No response
Would you like to implement a fix?
None
Hi @andersk
thanks for your report. Yes, this requires one more change in casting process. I will release it shortly.
I've the same issue, but using a different mimetype application/problem+json.
/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/deserializing/media_types/deserializers.py:24: UserWarning: Unsupported application/problem+json mimetype
warnings.warn(f"Unsupported {self.mimetype} mimetype")
Traceback (most recent call last):
File "/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/validation/decorators.py", line 31, in wrapper
return f(*args, **kwds)
File "/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/validation/request/validators.py", line 253, in _get_body
return self._get_content_value(raw_body, mimetype, content)
File "/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/unmarshalling/unmarshallers.py", line 113, in _get_content_value
return self._unmarshal_schema(schema, casted)
File "/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/unmarshalling/unmarshallers.py", line 90, in _unmarshal_schema
return unmarshaller.unmarshal(value)
File "/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/unmarshalling/schemas/unmarshallers.py", line 282, in unmarshal
self.schema_validator.validate(value)
File "/Users/raul/.pyenv/versions/3.7.17/lib/python3.7/site-packages/openapi_core/validation/schemas/validators.py", line 41, in validate
raise InvalidSchemaValue(value, schema_type, schema_errors=errors)
openapi_core.validation.schemas.exceptions.InvalidSchemaValue: Value foo=123 not valid for schema of type object: (<ValidationError: "'foo=123' is not of type 'object'">,)The allowed mime types from what i can see in the code are {'application/json', 'application/x-www-form-urlencoded', 'multipart/form-data'}. I was wondering if the PR will solve this and default to convert to object or I need to define a function for my custom mimetype?
@lRaulMN7 That’s not the same issue—before even getting to the 123, your foo=123 string would need to be parsed, and it’s hard to imagine how it could be parsed as a type that calls itself application/problem+json. If you still think there’s a bug there, I’d recommend opening a new issue including your code.