pythonfastapipython-typing

Why is this type annotation not showing up in FastAPI?


This is one of the endpoints in my FastAPI application:

@router.get("/", response_model=Page[DeviceConfigSchemaOUT])
async def get_device_configs(
    search: str | None = None,
    page: int = 1,
    size: int = 50,
) -> Any:
    ...

Which shows up like this in Swagger:

endpoint without type annotation

Why is there no type on the search parameter? I tried changing the declaration to an Annotation and manually specifying the type, but the result stays the same. This is using the latest FastAPI version 109.2 and Python 3.12.1.


Solution

  • It seems to be an issue in Pydantic 2 and FastAPI but there is a workaround as discussed in FastAPI discussions (link):

    from pydantic.json_schema import SkipJsonSchema
    
    @router.get("/", response_model=Page[DeviceConfigSchemaOUT])
    async def get_device_configs(
        search: str | SkipJsonSchema[None] = None,
        page: int = 1,
        size: int = 50,
    ) -> Any:
        ...