1

I'm trying to create a separate class with search parameters that occur in different routers in my application. When I add them to the class, the description does not appear in openapi

I created a separate class with parameters


from fastapi import Query, HTTPException


class PaginatedParams(BaseModel):
    page: Annotated[int, Query(1, description='Page number')]
    size: Annotated[int, Query(10, description='Number of records per page')]

    def validate(self):
        if self.page < 1:
            raise HTTPException(
                status_code=HTTPStatus.BAD_REQUEST,
                detail="Page number must be greater than 0",
            )
        if self.size < 1:
            raise HTTPException(
                status_code=HTTPStatus.BAD_REQUEST,
                detail="Size number must be greater than 0",
            )

Then I added my class to router


from fastapi import APIRouter, Depends, HTTPException, Query


@router.get(
    '/search',
    response_model=dict[str, list[FilmsListApi] | int | str],
    description='Search films by its title.',
)
async def search_films(
        params: PaginatedParams = Depends(PaginatedParams),
        query: str = Query('', description='The keywords for searching films'),
        film_service: FilmService = Depends(get_film_service),
) -> dict[str, list[FilmsListApi] | int | str]:

And I expected that at the api/openapi address the description of the parameters would be displayed in the same way as the query, but this does not happen

enter image description here

I tried changing the code in various ways but couldn't achieve anything. I think that the problem may lie in some place that I may not know about.

2

1 Answer 1

0

I think you can achieve that now (since FastAPI version 0.115.0), using the Query Parameter Models. Hope that helps!

https://fastapi.tiangolo.com/tutorial/query-param-models/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.