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

automatically handle 204 status code when not overriding twith custom… #2833

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions fastapi/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
request_response,
websocket_session,
)
from starlette.status import WS_1008_POLICY_VIOLATION
from starlette.status import HTTP_204_NO_CONTENT, WS_1008_POLICY_VIOLATION
from starlette.types import ASGIApp
from starlette.websockets import WebSocket

Expand Down Expand Up @@ -168,7 +168,11 @@ def get_request_handler(
is_coroutine = asyncio.iscoroutinefunction(dependant.call)
is_body_form = body_field and isinstance(body_field.field_info, params.Form)
if isinstance(response_class, DefaultPlaceholder):
actual_response_class: Type[Response] = response_class.value
actual_response_class: Type[Response]
if status_code == HTTP_204_NO_CONTENT:
actual_response_class = Response
else:
actual_response_class = response_class.value
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the type annotation was dropped here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pylance (Pyright) called this a redefinition, I'll check if mypy throws the same errors and if not I'll add type annotation to the else clasue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some solution, not best either but maybe better?

else:
actual_response_class = response_class

Expand Down
23 changes: 23 additions & 0 deletions tests/test_response_code_no_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ async def b():
pass # pragma: no cover


@app.get(
"/c",
status_code=204,
)
async def c():
pass # pragma: no cover


openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
Expand Down Expand Up @@ -70,6 +78,15 @@ async def b():
"operationId": "b_b_get",
}
},
"/c": {
"get": {
"responses": {
"204": {"description": "Successful Response"},
},
"summary": "C",
"operationId": "c_c_get",
}
},
},
"components": {
"schemas": {
Expand Down Expand Up @@ -106,3 +123,9 @@ def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == openapi_schema


def test_no_content():
response = client.get("/c")
assert response.status_code == 204
assert response.content == b""