Fix reading FastAPI request body twice. #1724
Merged
antonpirker merged 17 commits intomasterfrom Nov 10, 2022
Merged
Conversation
…tsentry/sentry-python into antonpirker/1675-fastapi-request-body
sl0thentr0py
requested changes
Nov 7, 2022
…tsentry/sentry-python into antonpirker/1675-fastapi-request-body
…time for experiments.
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
hasier
reviewed
Nov 9, 2022
| assert form_data.keys() == PARSED_FORM.keys() | ||
| assert form_data["username"] == PARSED_FORM["username"] | ||
| assert form_data["password"] == PARSED_FORM["password"] | ||
| assert form_data["photo"].filename == PARSED_FORM["photo"].filename |
Contributor
There was a problem hiding this comment.
Maybe call await request.stream() or body() right at the end to ensure that there are no regressions? Maybe it'd be relevant to do the same in the rest of tests as well, just to make sure in all cases we still have access to the whole request stream/body?
sl0thentr0py
reviewed
Nov 9, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Starlette/FastAPI is internally caching the request body if read via
request.json()orrequest.body()but NOT when usingrequest.form(). This leads to a problem when our Sentry Starlette integration wants to read the body data and also the users code wants to read the same data.Solution:
Force caching of request body for
.form()calls too, to prevent error when body is read twice.The tests where mocking
.stream()and thus hiding this problem. So the tests have been refactored to mock the underlying._receive()function instead.Fixes #1675