fix: resolve ForwardRef inside Annotated in get_typed_annotation#15364
Open
OfekDanny wants to merge 1 commit into
Open
fix: resolve ForwardRef inside Annotated in get_typed_annotation#15364OfekDanny wants to merge 1 commit into
OfekDanny wants to merge 1 commit into
Conversation
Contributor
|
This pull request has a merge conflict that needs to be resolved. |
When a type annotation uses a string literal inside Annotated, e.g.
Annotated['Potato', Depends(get_potato)], Python stores it as
Annotated[ForwardRef('Potato'), Depends(get_potato)] at runtime.
get_typed_annotation only handled the case where the annotation itself
is a string/ForwardRef. It did not recurse into Annotated's first
argument. As a result, the ForwardRef was never resolved, FastAPI
could not identify the type, and the parameter was treated as Any,
producing an incorrect OpenAPI schema.
Fix by also handling the case where annotation is ForwardRef directly
(not wrapped in a string), and by recursively resolving any
ForwardRef found as the first argument of an Annotated type.
Fixes fastapi#13056
2cebfe0 to
dcce6ec
Compare
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.
Problem
Using a string literal as the first argument of
Annotatedcauses FastAPI to treat the parameter asAny, producing an incorrect OpenAPI schema.This affects two related cases:
Case 1 — explicit string in
Annotated:Python automatically creates
Annotated[ForwardRef("Potato"), Depends(get_potato)]. Becauseget_typed_annotationonly resolved the annotation itself (if it was a top-level string), theForwardRefinsideAnnotatedwas never resolved.Case 2 —
from __future__ import annotationswithAnnotated:With PEP 563, annotations are stored as strings.
inspect.signature(call, eval_str=True)raisesNameError(ifPotatois defined later in the module), FastAPI falls back to the raw string,evaluate_forwardrefproducesAnnotated[ForwardRef("Potato"), Depends(...)]— and the same unresolved ForwardRef problem occurs.Result in both cases:
potatoappears as a plain query parameter with typeanyin the OpenAPI schema instead of being recognised as a dependency injection.Closes #13056
Fix
Two small changes to
get_typed_annotation:Handle
ForwardRefinputs directly (not juststr):Recursively resolve
ForwardRefinsideAnnotated's first argument:All needed imports (
Annotated,ForwardRef,get_args,get_origin) were already present.