Skip to content

Can't use Annotated with ForwardRef #13056

@Kludex

Description

@Kludex

Issue Content

The following code doesn't generate the correct OpenAPI json:

from __future__ import annotations

from dataclasses import dataclass
from typing import Annotated

from fastapi import Depends, FastAPI

app = FastAPI()


def get_potato() -> Potato:
    return Potato(color='red', size=10)


@app.get('/')
async def read_root(potato: Annotated[Potato, Depends(get_potato)]):
    return {'Hello': 'World'}


@dataclass
class Potato:
    color: str
    size: int

If we move the Potato up, or remove the Annotated, then it works as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions