From 7c1824d258706d09d7d5bf1409a6335373c3950b Mon Sep 17 00:00:00 2001 From: p1c2u Date: Sat, 12 Jun 2021 11:50:55 +0100 Subject: [PATCH] Django middleware error response validation fix --- openapi_core/contrib/django/middlewares.py | 9 +++---- tests/integration/data/v3.0/petstore.yaml | 31 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/openapi_core/contrib/django/middlewares.py b/openapi_core/contrib/django/middlewares.py index fc2fb23e..cd1356ff 100644 --- a/openapi_core/contrib/django/middlewares.py +++ b/openapi_core/contrib/django/middlewares.py @@ -31,11 +31,10 @@ def __call__(self, request): openapi_request = self._get_openapi_request(request) req_result = self.validation_processor.process_request(openapi_request) if req_result.errors: - return self._handle_request_errors(req_result, request) - - request.openapi = req_result - - response = self.get_response(request) + response = self._handle_request_errors(req_result, request) + else: + request.openapi = req_result + response = self.get_response(request) openapi_response = self._get_openapi_response(response) resp_result = self.validation_processor.process_response( diff --git a/tests/integration/data/v3.0/petstore.yaml b/tests/integration/data/v3.0/petstore.yaml index e4beec6c..dbebd363 100644 --- a/tests/integration/data/v3.0/petstore.yaml +++ b/tests/integration/data/v3.0/petstore.yaml @@ -93,6 +93,8 @@ paths: responses: '200': $ref: "#/components/responses/PetsResponse" + '400': + $ref: "#/components/responses/ErrorResponse" '404': $ref: "#/components/responses/HtmlResponse" post: @@ -403,6 +405,31 @@ components: default: 400 message: type: string + StandardError: + type: object + x-model: StandardError + required: + - title + - status + - class + properties: + title: + type: string + status: + type: integer + format: int32 + default: 400 + class: + type: string + StandardErrors: + type: object + required: + - errors + properties: + errors: + type: array + items: + $ref: "#/components/schemas/StandardError" ExtendedError: type: object x-model: ExtendedError @@ -430,7 +457,9 @@ components: content: application/json: schema: - $ref: "#/components/schemas/ExtendedError" + oneOf: + - $ref: "#/components/schemas/StandardErrors" + - $ref: "#/components/schemas/ExtendedError" HtmlResponse: description: HTML page content: