fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

✨ Don't revalidate the response content if it is of the same type as `response_model`

Open kaiix opened this issue 3 years ago • 9 comments

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

kaiix avatar Jan 12 '22 08:01 kaiix

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.

codecov[bot] avatar Sep 11 '22 16:09 codecov[bot]

📝 Docs preview for commit 301a113722119644bd219b1ad675a838305526cb at: https://631e12eab9acb84c145aec85--fastapi.netlify.app

github-actions[bot] avatar Sep 11 '22 16:09 github-actions[bot]

📝 Docs preview for commit a9b3e8cbfca779593edce57d8ae6e1f9d0009359 at: https://639cd42098600317dc000812--fastapi.netlify.app

github-actions[bot] avatar Dec 16 '22 20:12 github-actions[bot]

@tiangolo any chance this PR will be reviewed?

dolfinus avatar Mar 05 '23 19:03 dolfinus

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?

Tishka17 avatar Mar 05 '23 19:03 Tishka17

@tiangolo hi! Can you answer on this comment?

https://github.com/tiangolo/fastapi/pull/4416#issuecomment-1455178994

nkhitrov avatar May 25 '23 18:05 nkhitrov

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!

levsh avatar Jun 06 '23 09:06 levsh

@tiangolo are there any plans on merging or reimplementing this?

Tishka17 avatar Jul 06 '23 08:07 Tishka17

@tiangolo is there any way this PR could be merged?

Danipulok avatar Jul 06 '23 08:07 Danipulok