Skip to content

Commit

Permalink
Can return HTTPStatus member when default response is declared (#1918)
Browse files Browse the repository at this point in the history
Fixes #1917  .



Changes proposed in this pull request:

 - Look at enum value for return status code if given
 -
 -

---------

Co-authored-by: Ruwann <[email protected]>
  • Loading branch information
chbndrhnns and Ruwann committed May 30, 2024
1 parent 823fa6c commit a930303
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion connexion/decorators/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ 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
# 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
):
Expand Down
7 changes: 7 additions & 0 deletions tests/api/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
5 changes: 5 additions & 0 deletions tests/fakeapi/hello/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import datetime
import uuid
from http import HTTPStatus

import flask
from connexion import NoContent, ProblemException, context, request
Expand Down Expand Up @@ -737,3 +738,7 @@ def get_streaming_response():

async def async_route():
return {}, 200


def httpstatus():
return {}, HTTPStatus.CREATED
18 changes: 17 additions & 1 deletion tests/fixtures/simple/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit a930303

Please sign in to comment.