Skip to content

[Bug]: Can’t validate integer/number/boolean from application/x-www-form-urlencoded request body #700

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

Closed
andersk opened this issue Oct 21, 2023 · 4 comments · Fixed by #701
Labels
kind/bug Indicates an issue

Comments

@andersk
Copy link
Contributor

andersk commented Oct 21, 2023

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 error

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": "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

@andersk andersk added the kind/bug Indicates an issue label Oct 21, 2023
@p1c2u
Copy link
Collaborator

p1c2u commented Oct 22, 2023

Hi @andersk

thanks for your report. Yes, this requires one more change in casting process. I will release it shortly.

@p1c2u p1c2u mentioned this issue Oct 22, 2023
@lRaulMN7
Copy link

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?

@andersk
Copy link
Contributor Author

andersk commented Oct 23, 2023

@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.

@lRaulMN7
Copy link

Thanks @andersk , you guided me to the solution! I had to use extra_media_type_deserializers as explained in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Indicates an issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants