Repository navigation
fix(cache): send the headers and cookies dependencies set on every response (#233) - #435
Merged
Merged
Conversation
…sponse (#233) FastAPI merges the shared sub-Response into the result only when the endpoint returns plain data, and the @cache wrapper always returns a Response. A dependency's headers and cookies were lost on a handler without a `response: Response` parameter; with one, they were stored with the entry and replayed on every hit with the values of the request that filled the cache. The wrapper now always receives the sub-response (it injects the parameter when the handler does not declare one) and records the lines the dependencies set before the handler runs. Only the lines the handler adds are part of its response and stored; the dependencies' lines are added to every response the wrapper sends, miss, hit and 304. A cookie a dependency sets keeps the response out of the backend and makes it private, as a cookie the handler sets does.
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.
Closes #233.
FastAPI hands every dependency and the handler one sub-
Response, and merges it into the result only when the endpoint returns plain data.@cachealways returns aResponse, so before this change:response: Responselost every header and cookie its dependencies set;What changes
Responseparameter,@cacheinjects a keyword-only__cachex_response. At wrapper entry the dependencies have already run, so the sub-response lines at that point are the dependencies' lines. Only the lines the handler adds after that are stored.private/no-storeCache-Control, counts as if the handler had set it. The response is not stored, and it is sent with that header or withprivate.Cache-Controlis handled like the handler's own: the decorator's replaces it, and a bare@cache()keeps it (@cache() with no directives sends an empty Cache-Control header #363).Annotated[Response, Depends(...)]andresponse: Response = Depends(...)are dependency results, not the sub-response, so the injection still happens for them.get_responsekeeps its signature as a compatibility wrapper.Behaviour changes (
233.changed.md)@cacheroute it applies to out of the backend. The docs add a note on this.@cachealso adds the dependencies' headers to aResponsethe handler returns itself. FastAPI does not do this.Review
An independent read-only review ran twice:
privateheader no longer prevented storage. That is fixed and tested.Dependsform was taken for the sub-response.@cache()dropped a dependency'sCache-Control.Each new test was mutation-checked: reverting the code it guards makes it fail.
Checks
mypy testsandmypy scripts--strict