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

improved error 415 #1185

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def wrapper(request):
else:
# the body has contents that were not parsed as JSON
raise UnsupportedMediaTypeProblem(
"Invalid Content-type ({content_type}), expected JSON data".format(
detail="Invalid Content-type ({content_type}), expected JSON data".format(
content_type=request.headers.get("Content-Type", "")
))

Expand Down
8 changes: 8 additions & 0 deletions tests/api/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,11 @@ def test_errors(problem_app):
problem_as_exception_body = json.loads(problem_as_exception.data.decode('utf-8', 'replace'))
assert 'age' in problem_as_exception_body
assert problem_as_exception_body['age'] == 30

unsupported_media_type = app_client.post('/v1.0/post_wrong_content_type', data='<html></html>', content_type='text/html')
assert unsupported_media_type.status_code == 415
unsupported_media_type_body = json.loads(unsupported_media_type.data.decode('utf-8', 'replace'))
assert unsupported_media_type_body['type'] == 'about:blank'
assert unsupported_media_type_body['title'] == 'Unsupported Media Type'
assert unsupported_media_type_body['detail'] == 'Invalid Content-type (text/html), expected JSON data'
assert unsupported_media_type_body['status'] == 415
12 changes: 12 additions & 0 deletions tests/fixtures/problem/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ paths:
responses:
'200':
description: Problem exception
/post_wrong_content_type:
post:
description: Unsupported media type
operationId: fakeapi.hello.post_wrong_content_type
requestBody:
content:
application/json:
schema:
type: object
responses:
200:
description: OK
servers:
- url: /v1.0
components:
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/problem/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,19 @@ paths:
responses:
200:
description: Problem exception

/post_wrong_content_type:
post:
description: Unsupported media type
operationId: fakeapi.hello.post_wrong_content_type
consumes:
- application/json
parameters:
- in: body
name: body
description: The request body
schema:
type: object
responses:
200:
description: OK