Skip to content
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

[Bug]: Won’t validate empty request body even if it satisfies the schema #702

Closed
andersk opened this issue Oct 23, 2023 · 1 comment · Fixed by #713
Closed

[Bug]: Won’t validate empty request body even if it satisfies the schema #702

andersk opened this issue Oct 23, 2023 · 1 comment · Fixed by #713
Labels

Comments

@andersk
Copy link
Contributor

andersk commented Oct 23, 2023

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

@andersk andersk added the kind/bug Indicates an issue label Oct 23, 2023
@andersk
Copy link
Contributor Author

andersk commented Oct 23, 2023

Oh additionally, requestBody.required is supposed to default to false. (But I’m pretty sure an empty body that meets the schema should be allowed even if required is true?)

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