fastapi
fastapi copied to clipboard
✨ Don't revalidate the response content if it is of the same type as `response_model`
There's no need to use secure_cloned_response_field
to revalidate the response_content
if the response object type is exactly the same as response_model
related issues:
- https://github.com/tiangolo/fastapi/issues/2359
- https://github.com/tiangolo/fastapi/issues/2689
- https://github.com/tiangolo/fastapi/issues/3021
- https://github.com/tiangolo/fastapi/issues/3454
- https://github.com/tiangolo/fastapi/issues/3503
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Comparison is base (
cf73051
) 100.00% compared to head (301a113
) 100.00%. Report is 1038 commits behind head on master.
:exclamation: Current head 301a113 differs from pull request most recent head a9b3e8c. Consider uploading reports for the commit a9b3e8c to get more accurate results
Additional details and impacted files
@@ Coverage Diff @@
## master #4416 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 540 540
Lines 13969 13965 -4
=========================================
- Hits 13969 13965 -4
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
📝 Docs preview for commit 301a113722119644bd219b1ad675a838305526cb at: https://631e12eab9acb84c145aec85--fastapi.netlify.app
📝 Docs preview for commit a9b3e8cbfca779593edce57d8ae6e1f9d0009359 at: https://639cd42098600317dc000812--fastapi.netlify.app
@tiangolo any chance this PR will be reviewed?
Rebuilding a response model is a real problem and cannot actually be done correctly because of aliases and other settings (I guess I've already left a comment about this). In normal code your create it by your own from the other sources that could not be converted automatically so it is actually not needed.
Can we expect this to be reviewed and fixed?
@tiangolo hi! Can you answer on this comment?
https://github.com/tiangolo/fastapi/pull/4416#issuecomment-1455178994
Hi!
I have this problem too (
This can greatly degrade performance for the complex models with validators or endpoints that return a large number of items.
In example below get_items
endpoint already returns a list of items so it is no need to rebuild response again.
And finally we have response in ~2 second instead of ~1 second.
from typing import List
from uuid import UUID, uuid4
from fastapi import FastAPI
from pydantic import BaseModel, validator
app = FastAPI()
class Data(BaseModel):
i: int
@validator("i")
def validate_i(cls, v):
sleep(0.1)
return v
class Item(BaseModel):
id: UUID
data: Data
@app.get(
"/items",
response_model=List[Item],
)
def get_items():
return [Item(id=uuid4(), data=Data(i=i)) for i in range(10)]
Thank you!
@tiangolo are there any plans on merging or reimplementing this?
@tiangolo is there any way this PR could be merged?