Fix ForwardRef resolution inside Annotated in get_typed_annotation#15481
Draft
siddharth10ss wants to merge 2 commits into
Draft
Fix ForwardRef resolution inside Annotated in get_typed_annotation#15481siddharth10ss wants to merge 2 commits into
siddharth10ss wants to merge 2 commits into
Conversation
YuriiMotov
reviewed
May 4, 2026
Comment on lines
+42
to
+56
| def test_annotated_forwardref_response() -> None: | ||
| """Endpoint using Annotated[ForwardRef, Depends(...)] must return 200.""" | ||
| response = client.get("/") | ||
| assert response.status_code == 200 | ||
| assert response.json() == {"color": "red", "size": 10} | ||
|
|
||
|
|
||
| def test_annotated_forwardref_openapi_schema() -> None: | ||
| """OpenAPI schema must be generated without errors and must not leak ForwardRef.""" | ||
| response = client.get("/openapi.json") | ||
| assert response.status_code == 200 | ||
| schema_text = response.text | ||
| assert "ForwardRef" not in schema_text, ( | ||
| "Unresolved ForwardRef leaked into the OpenAPI schema" | ||
| ) |
Member
|
Let's keep it as draft until CI passes. |
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.
Fix ForwardRef resolution inside Annotated (Closes #13056)
Problem
When using
from __future__ import annotations, type annotations are stored as strings (ForwardRef). FastAPI resolves forward references when the annotation itself is a string, but fails when the ForwardRef is wrapped insidetyping.Annotated.Example:
In this case,
"Potato"is not resolved correctly, which leads to incorrect dependency parsing and incorrect OpenAPI schema generation.Root Cause
get_typed_annotation()only resolves forward references when the annotation is directly astrorForwardRef. It does not handle ForwardRefs nested insideAnnotated.Solution
Annotatedtypes usingget_originget_argsstrandForwardRef)Annotatedusing__class_getitem__to preserve correct structureChanges
AnnotatedAnnotatedcasesNoneTypeTests
Added test to validate:
Annotated[Potato, Depends(...)](existing behavior unchanged)Annotated["Potato", Depends(...)](ForwardRef correctly resolved)Notes
get_typed_annotation()Closes #13056