Closed
Description
Actual Behavior
OpenAPI allows specifying an application/x-www-form-urlencoded
request body that contains individual application/json
encoded properties, by using the contentType
field of the encoding object. The Swagger specification shows an example of this (under “Complex Serialization in Form Data”, and reproduced below). However, openapi-core ignores the contentType
field and fails to validate a valid request against such a spec.
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 253, in _get_body
return self._get_content_value(raw_body, mimetype, content)
File "/home/anders/python/openapi-core/openapi_core/validation/validators.py", line 126, in _get_content_value
self._validate_schema(schema, casted)
File "/home/anders/python/openapi-core/openapi_core/validation/validators.py", line 102, 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 {'payload': '{"text":"Swagger is awesome"}'} not valid for schema of type object: (<ValidationError: '\'{"text":"Swagger is awesome"}\' is not of type \'object\''>,)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/anders/python/openapi-core/post_encoding_test.py", line 65, in <module>
validate_request(request, spec=spec)
File "/home/anders/python/openapi-core/openapi_core/shortcuts.py", line 305, in validate_request
validate_apicall_request(
File "/home/anders/python/openapi-core/openapi_core/shortcuts.py", line 379, in validate_apicall_request
return v.validate(request)
File "/home/anders/python/openapi-core/openapi_core/validation/request/validators.py", line 269, in validate
raise err
File "/home/anders/python/openapi-core/openapi_core/validation/request/validators.py", line 110, 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
This request should successfully validate.
Steps to Reproduce
import yaml
from urllib.parse import urlencode
from openapi_core import Spec
from openapi_core import validate_request
from openapi_core.testing import MockRequest
SPEC = r"""
openapi: 3.0.0
info:
version: 1.0.0
title: Slack Incoming Webhook
externalDocs:
url: https://api.slack.com/incoming-webhooks
servers:
- url: https://hooks.slack.com
paths:
/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX:
post:
summary: Post a message to Slack
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Message'
application/x-www-form-urlencoded:
schema:
type: object
properties:
payload: # <--- form field that contains the JSON message
$ref: '#/components/schemas/Message'
encoding:
payload:
contentType: application/json
responses:
'200':
description: OK
components:
schemas:
Message:
title: A Slack message
type: object
properties:
text:
type: string
description: Message text
required:
- text
"""
spec = Spec.from_dict(yaml.safe_load(SPEC))
request = MockRequest(
"https://hooks.slack.com/",
"post",
"/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
data=urlencode({"payload": '{"text":"Swagger is awesome"}'}),
mimetype="application/x-www-form-urlencoded",
)
validate_request(request, spec=spec)
OpenAPI Core Version
0.18.0
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