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

Conversation

aviramha
Copy link
Contributor

@aviramha aviramha commented Feb 18, 2021

… response class
related to #1458
related to #2832
related to: encode/httpx#1474

@github-actions
Copy link
Contributor

📝 Docs preview for commit d764fba at: https://602e657f13921611efaf3e78--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit 8b30b9f at: https://602e66cf025bf7197c121ffd--fastapi.netlify.app

@github-actions
Copy link
Contributor

📝 Docs preview for commit b3aa37d at: https://602e6b07ec1288f69e2e539d--fastapi.netlify.app

@codecov
Copy link

codecov bot commented Feb 18, 2021

Codecov Report

Merging #2833 (f704126) into master (a7a353e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2833   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          243       243           
  Lines         7419      7427    +8     
=========================================
+ Hits          7419      7427    +8     
Impacted Files Coverage Δ
fastapi/routing.py 100.00% <100.00%> (ø)
tests/test_response_code_no_body.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 33be5fc...f704126. Read the comment docs.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 06ff6a2 at: https://602e7299b2a3060696389d6c--fastapi.netlify.app

if status_code == HTTP_204_NO_CONTENT:
actual_response_class: Type[Response] = 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?

@@ -168,7 +168,10 @@ 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
if status_code == HTTP_204_NO_CONTENT:
actual_response_class: Type[Response] = Response
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

This seems to work as expected, even when you override the response status code, as documented here: https://fastapi.tiangolo.com/advanced/response-change-status-code/#response-change-status-code

I'm not opposed to this, but I wish we didn't have to add another conditional here, and it seems like we might get some unintended behavior, as we're really just taking advantage of the fact that Response converts None to an empty byte string, not that we're actually doing any validation of the result (I'm not proposing that we should be... either).

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 understand your concern, yet I didn't find any more gentle solution.. feel free to propose if you have any :)

@github-actions
Copy link
Contributor

📝 Docs preview for commit f704126 at: https://602fcc91101e6627f5e32cc1--fastapi.netlify.app

@dongfangtianyu
Copy link

Hi,
I just met "too much data for declared content length"
What is the current status of this PR? What can I do somethings ?

@kekel87
Copy link

kekel87 commented Jun 14, 2021

Hello. Some news about this MR ? 😅 (we lost a lot of time because of this bug)

@funkindy
Copy link

Hi, any updates on this PR?

@TBBle TBBle mentioned this pull request Nov 26, 2021
@TBBle
Copy link

TBBle commented Nov 26, 2021

With only a brief glance, it seems this won't work if you set the status code using the temporary Response object approach, as you'd have to repeat the check after checking sub_response.status_code later in the same function.

So for example, this idiom would still slip "null" out as the response body in violation of the RFC.

@app.get(
    "/d",
    status_code=200,
    responses={
        200: {}
        204: {}
    }
)
async def d(response: Response):
    if do_not_return_content():
        response.status_code = 204
        return
    return {"field": "value" }

@TBBle
Copy link

TBBle commented Jul 19, 2022

Is this PR superseded by #5145?

@tiangolo
Copy link
Owner

tiangolo commented Sep 3, 2022

It seems this was fixed in #5145, right? That fix is available since FastAPI 0.79.0.

I just tried the test you have here and it seems to pass with the current code, so I would think this is already handled. 🎉

But let me know if you think there's something else missing.

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@github-actions github-actions bot closed this Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants