fix(cache): render non-Response results the way FastAPI does - #135
Merged
Merged
Conversation
get_response() built the response with response_class(content=result), which skipped the response model, the route's status_code and anything set on an injected response: Response. Serialize through the response model (or jsonable_encoder), apply the route/sub-response status, blank the body for statuses that allow none, and merge the sub-response headers. Closes #99
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 #99.
Problem
When a
@cachehandler returned plain data,get_response()built the response withresponse_class(content=result)and handed FastAPI a finishedResponse, so FastAPI skipped its own handling of the return value:datetimeorUUIDfailed to encode (500), andresponse_modelfiltering never ran.@app.get(..., status_code=202)answered 200.response: Responsewere lost.Change
In
get_response(), for non-Responseresults:_serialize_result(). When the route has a response model (declared, or inferred from the return annotation), the result is validated with apydantic.TypeAdapter, usingfrom_attributes=Trueso a different model such asUserIn→UserOutworks. It is then dumped withmode="json"and the route'sresponse_model_include/exclude/by_alias/exclude_unset/exclude_defaults/exclude_none. Otherwise the result goes throughjsonable_encoder.APIRouteis unhashable, so the adapter is stored once per route as an attribute instead of being kept in a dict.status_codeapplies, overridden by the injected sub-response's status when the handler set one. This matches FastAPI's_build_response_args. A status that allows no body (for example 204) gets an empty body.Responseinstance among the handler's kwargs, and its raw headers are appended.Cache entries already store the status code and headers, so hits replay them.
Set-Cookieis still never stored.I used public APIs (
TypeAdapter,jsonable_encoder,fastapi.utils.is_body_allowed_for_status_code) rather than callingfastapi.routing.serialize_response. Its signature changes between FastAPI releases, and the dependency is unpinned.Tests
The new
tests/test_cache_rendering.pycovers:response_model_exclude_none;status_codeon miss and hit;All 5 fail against the old
get_response().Checks
fastapi_cachex,testsandscriptstestsandscriptsCACHEX_REQUIRE_LIVE_SERVERS=1): 725 passed, 100% coveragezensical build --strictdocs/HTTP_CACHING.mdgains a paragraph on how plain return values are rendered. The CHANGELOG entry is at the top of### Fixed, a spot the open PRs #132 and #133 don't touch.