From 39da1f2186141c7405aeff6646f66ccce8ab5643 Mon Sep 17 00:00:00 2001 From: chbndrhnns Date: Tue, 30 Apr 2024 14:24:00 +0200 Subject: [PATCH 1/3] Can return HTTPStatus member when default response is declared --- connexion/middleware/response_validation.py | 6 +++++- tests/api/test_responses.py | 7 +++++++ tests/fakeapi/hello/__init__.py | 5 +++++ tests/fixtures/simple/openapi.yaml | 18 +++++++++++++++++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/connexion/middleware/response_validation.py b/connexion/middleware/response_validation.py index ea0baf7cf..62dc2e918 100644 --- a/connexion/middleware/response_validation.py +++ b/connexion/middleware/response_validation.py @@ -97,7 +97,11 @@ async def wrapped_send(message: t.MutableMapping[str, t.Any]) -> None: if message["status"] < 400: self.validate_mime_type(mime_type) - status = str(message["status"]) + try: + status = str(message["status"].value) + except AttributeError: + status = str(message["status"]) + response_definition = self._operation.response_definition( status, mime_type ) diff --git a/tests/api/test_responses.py b/tests/api/test_responses.py index e9649bf0c..afcb39c49 100644 --- a/tests/api/test_responses.py +++ b/tests/api/test_responses.py @@ -140,6 +140,13 @@ def test_pass_through(simple_app): ) +def test_can_use_httpstatus_enum(simple_openapi_app): + app_client = simple_openapi_app.test_client() + + response = app_client.get("/v1.0/httpstatus") + assert response.status_code == 201 + + def test_empty(simple_app): app_client = simple_app.test_client() diff --git a/tests/fakeapi/hello/__init__.py b/tests/fakeapi/hello/__init__.py index de2af8fdd..b6d6c00ac 100644 --- a/tests/fakeapi/hello/__init__.py +++ b/tests/fakeapi/hello/__init__.py @@ -1,6 +1,7 @@ import asyncio import datetime import uuid +from http import HTTPStatus import flask from connexion import NoContent, ProblemException, context, request @@ -737,3 +738,7 @@ def get_streaming_response(): async def async_route(): return {}, 200 + + +def httpstatus(): + return {}, HTTPStatus.CREATED diff --git a/tests/fixtures/simple/openapi.yaml b/tests/fixtures/simple/openapi.yaml index c4ce61773..e183a1cb9 100644 --- a/tests/fixtures/simple/openapi.yaml +++ b/tests/fixtures/simple/openapi.yaml @@ -1316,7 +1316,23 @@ paths: responses: 200: description: 'OK' - + /httpstatus: + get: + operationId: fakeapi.hello.httpstatus + responses: + 201: + description: "happy path" + default: + description: "default" + content: + application/json: + schema: + type: object + properties: + error_code: + type: integer + required: + - error_code servers: - url: http://localhost:{port}/{basePath} From b7ec3da9aa599037041f43e0de5039f5f9f2aad7 Mon Sep 17 00:00:00 2001 From: chbndrhnns Date: Tue, 28 May 2024 17:02:19 +0200 Subject: [PATCH 2/3] Convert status code to int to allow HTTPStatus members --- connexion/decorators/response.py | 2 +- connexion/middleware/response_validation.py | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/connexion/decorators/response.py b/connexion/decorators/response.py index 160ace61c..97662c3a9 100644 --- a/connexion/decorators/response.py +++ b/connexion/decorators/response.py @@ -138,7 +138,7 @@ def _unpack_handler_response( elif len(handler_response) == 2: data, status_code_or_headers = handler_response if isinstance(status_code_or_headers, int): - status_code = status_code_or_headers + status_code = int(status_code_or_headers) elif isinstance(status_code_or_headers, Enum) and isinstance( status_code_or_headers.value, int ): diff --git a/connexion/middleware/response_validation.py b/connexion/middleware/response_validation.py index 62dc2e918..ea0baf7cf 100644 --- a/connexion/middleware/response_validation.py +++ b/connexion/middleware/response_validation.py @@ -97,11 +97,7 @@ async def wrapped_send(message: t.MutableMapping[str, t.Any]) -> None: if message["status"] < 400: self.validate_mime_type(mime_type) - try: - status = str(message["status"].value) - except AttributeError: - status = str(message["status"]) - + status = str(message["status"]) response_definition = self._operation.response_definition( status, mime_type ) From 16e2b481948bcb0ab310062465d9faea38f28f61 Mon Sep 17 00:00:00 2001 From: Ruwann Date: Thu, 30 May 2024 09:15:33 +0200 Subject: [PATCH 3/3] Update connexion/decorators/response.py --- connexion/decorators/response.py | 1 + 1 file changed, 1 insertion(+) diff --git a/connexion/decorators/response.py b/connexion/decorators/response.py index 97662c3a9..0aa58870d 100644 --- a/connexion/decorators/response.py +++ b/connexion/decorators/response.py @@ -138,6 +138,7 @@ def _unpack_handler_response( elif len(handler_response) == 2: data, status_code_or_headers = handler_response if isinstance(status_code_or_headers, int): + # Extra int call because of int subclasses such as http.HTTPStatus (IntEnum) status_code = int(status_code_or_headers) elif isinstance(status_code_or_headers, Enum) and isinstance( status_code_or_headers.value, int