Fix issue #13056: Handle ForwardRef in Annotated types by veeceey · Pull Request #14872 · fastapi/fastapi

@veeceey

When using Annotated types with ForwardRef string annotations (e.g.,
Annotated['ModelName', Query()]), FastAPI would fail with a Pydantic
error about the type not being fully defined.

The fix resolves ForwardRef in the extracted type annotation during
parameter analysis, ensuring that the actual resolved type is passed
to Pydantic rather than the unresolved ForwardRef.

Changes:
- Modified analyze_param() to resolve ForwardRef when extracting from Annotated
- Added globalns parameter to pass the function's global namespace for resolution
- Updated get_dependant() to extract and pass globalns to analyze_param()
- Added comprehensive tests for ForwardRef with Annotated types

The fix handles various scenarios:
- Query parameters with ForwardRef
- Path parameters with ForwardRef
- Header parameters with ForwardRef
- Request body with ForwardRef
- Optional types with ForwardRef
- Multiple annotations with ForwardRef