Repository navigation
🐛 Ignore Response classes on return annotation - #5855
Conversation
|
📝 Docs preview for commit bcde7e9 at: https://63ba7ea82674445278edeab6--fastapi.netlify.app |
bcde7e9 to
7ef60c7
Compare
7ef60c7 to
cae4af5
Compare
|
📝 Docs preview for commit cae4af5 at: https://63ba85339a71f156a9041f93--fastapi.netlify.app |
|
📝 Docs preview for commit 324bfce at: https://63bd4143d3db0f08a0b3e678--fastapi.netlify.app |
Kludex
left a comment
There was a problem hiding this comment.
I think the best approach would be to not accept this, revert to the previous release, and with more time, release the feature again with more time to think about the corner cases. 👀
| return_annotation = get_typed_return_annotation(endpoint) | ||
| if get_origin(return_annotation) is Union: | ||
| union_args = get_args(return_annotation) | ||
| args = tuple(arg for arg in union_args if not issubclass(arg, Response)) | ||
| if len(args) > 1: | ||
| response_model = Union[args] | ||
| elif len(args) == 1: | ||
| response_model = args[0] | ||
| else: | ||
| response_model = None | ||
| elif issubclass(return_annotation, Response): | ||
| response_model = None | ||
| else: | ||
| response_model = return_annotation |
There was a problem hiding this comment.
Those are the cases we talked about:
- When there's a single
Responseclassresponse_modelbecomesNone. - If it's an
Union, we filter theResponseclasses. (I'm not sure if this is what we want ???)
There was a problem hiding this comment.
Yeah, this gets quite complicated. Also because a Union could contain internal Unions, etc.
I think just the lenient_issubclass(return_annotation, Response), to cover the simplest use cases.
And then I'll add a bit to the docs explaining that if the user wants to return and annotate something different than a Pydantic field, it needs a response_model=None.
324bfce to
920f95e
Compare
920f95e to
f612ac1
Compare
|
📝 Docs preview for commit f612ac1 at: https://63bd4390143ac804b98c706a--fastapi.netlify.app |
| return_annotation = get_typed_return_annotation(endpoint) | ||
| if get_origin(return_annotation) is Union: | ||
| union_args = get_args(return_annotation) | ||
| args = tuple(arg for arg in union_args if not issubclass(arg, Response)) | ||
| if len(args) > 1: | ||
| response_model = Union[args] | ||
| elif len(args) == 1: | ||
| response_model = args[0] | ||
| else: | ||
| response_model = None | ||
| elif issubclass(return_annotation, Response): | ||
| response_model = None | ||
| else: | ||
| response_model = return_annotation |
There was a problem hiding this comment.
Yeah, this gets quite complicated. Also because a Union could contain internal Unions, etc.
I think just the lenient_issubclass(return_annotation, Response), to cover the simplest use cases.
And then I'll add a bit to the docs explaining that if the user wants to return and annotate something different than a Pydantic field, it needs a response_model=None.
|
Thank you! The other thing that could be done later would be to update the error warning about this. But I can probably do that in another PR later. |
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855 Signed-off-by: Nick Stenning <nick@whiteink.com>
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855 Signed-off-by: Nick Stenning <nick@whiteink.com>
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855 Signed-off-by: Nick Stenning <nick@whiteink.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855 Signed-off-by: Nick Stenning <nick@whiteink.com>
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855 Signed-off-by: Nick Stenning <nick@whiteink.com>
FastAPI 0.89.0 has a bug which causes the model to fail to start if handlers annotate a response class of Response (or its subclasses). See: fastapi/fastapi#5855 Signed-off-by: Nick Stenning <nick@whiteink.com>
Uh oh!
There was an error while loading. Please reload this page.