From fc6803bc0021c8b9840558d9429bb4c272689ea4 Mon Sep 17 00:00:00 2001 From: Parth Shah <43181887+parthsujalshah@users.noreply.github.com> Date: Fri, 5 Nov 2021 01:21:16 +0530 Subject: [PATCH] improved error 415 (#1185) * improved error 415 * test added * Fix tests for updated 415 error response Co-authored-by: unknown Co-authored-by: Ruwan --- connexion/decorators/validation.py | 2 +- tests/api/test_errors.py | 8 ++++++++ tests/fixtures/problem/openapi.yaml | 12 ++++++++++++ tests/fixtures/problem/swagger.yaml | 16 ++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/connexion/decorators/validation.py b/connexion/decorators/validation.py index 1f11ca7aa..e30f90a03 100644 --- a/connexion/decorators/validation.py +++ b/connexion/decorators/validation.py @@ -158,7 +158,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", "") )) diff --git a/tests/api/test_errors.py b/tests/api/test_errors.py index a8963bb08..1ad2a8f82 100644 --- a/tests/api/test_errors.py +++ b/tests/api/test_errors.py @@ -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='', 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 diff --git a/tests/fixtures/problem/openapi.yaml b/tests/fixtures/problem/openapi.yaml index 4e6e1fb4f..3bc02cd0f 100644 --- a/tests/fixtures/problem/openapi.yaml +++ b/tests/fixtures/problem/openapi.yaml @@ -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: diff --git a/tests/fixtures/problem/swagger.yaml b/tests/fixtures/problem/swagger.yaml index 121ad943d..c4e0f2b4a 100644 --- a/tests/fixtures/problem/swagger.yaml +++ b/tests/fixtures/problem/swagger.yaml @@ -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