Browse Source

fix: resolve ForwardRef in get_typed_annotation for Annotated+Depends (#13056)

When using from __future__ import annotations with Annotated[Potato, Depends(...)],
the annotation can reach analyze_param as a ForwardRef. Previously only str was
resolved; ForwardRef was returned as-is, causing Depends to be missed and the
param to be treated as a Query param. This led to PydanticUserError during
OpenAPI schema generation.

Extend get_typed_annotation to also resolve ForwardRef before returning.
pull/15142/head
aayushbaluni 4 months ago
parent
commit
3e93e805f6
  1. 1
      fastapi/dependencies/utils.py

1
fastapi/dependencies/utils.py

@ -245,6 +245,7 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
def get_typed_annotation(annotation: Any, globalns: dict[str, Any]) -> Any:
if isinstance(annotation, str):
annotation = ForwardRef(annotation)
if isinstance(annotation, ForwardRef):
annotation = evaluate_forwardref(annotation, globalns, globalns) # ty: ignore[deprecated]
if annotation is type(None):
return None

Loading…
Cancel
Save