Closed
Description
Actual Behavior
An empty application/x-www-form-urlencoded
request body unexpectedly fails to validate even if it should satisfy the schema, such as when all parameters are optional.
Traceback (most recent call last):
File "/home/anders/python/openapi-core/test.py", line 56, in <module>
validate_request(request, spec=spec) # error
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.content_type, operation)
File "/home/anders/python/openapi-core/openapi_core/validation/decorators.py", line 35, in wrapper
self._raise_error(exc, self.err_cls, f, *args, **kwds)
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 259, in _get_body
raw_body = self._get_body_value(body, request_body)
File "/home/anders/python/openapi-core/openapi_core/validation/request/validators.py", line 269, in _get_body_value
raise MissingRequestBody
openapi_core.validation.request.exceptions.MissingRequestBody: Missing request body
Expected 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": "string"},
"bar": {"type": "string"},
},
},
}
}
},
"responses": {"200": {"description": "OK"}},
}
}
},
}
)
request = MockRequest(
"http://localhost",
"post",
"/test",
data="foo=foo",
content_type="application/x-www-form-urlencoded",
)
validate_request(request, spec=spec) # ok
request = MockRequest(
"http://localhost",
"post",
"/test",
data="bar=bar",
content_type="application/x-www-form-urlencoded",
)
validate_request(request, spec=spec) # ok
request = MockRequest(
"http://localhost",
"post",
"/test",
data="",
content_type="application/x-www-form-urlencoded",
)
validate_request(request, spec=spec) # error
OpenAPI Core Version
current Git (efaa5ac)
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