Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set description for query parameter in swagger doc using Pydantic model (FastAPI)

Tags:

python

fastapi

This is continue to this question.

I have added a model to get query params to pydantic model

class QueryParams(BaseModel):
    x: str = Field(description="query x")
    y: str = Field(description="query y")
    z: str = Field(description="query z")


@app.get("/test-query-url/{test_id}")
async def get_by_query(test_id: int, query_params: QueryParams = Depends()):
    print(test_id)
    print(query_params.dict(by_alias=True))
    return True

it is working as expected but description(added in model) is not reflecting in swagger ui

enter image description here

But if same model is used for request body, then description is shown in swagger

enter image description here

Am I missing anything to get the description for QueryParams(model) in swagger ui?

like image 607
karsas Avatar asked Oct 15 '20 03:10

karsas


People also ask

How do I define a path parameter in Swagger?

In Swagger, a path parameter is defined using in: path and other attributes as necessary. The parameter name must be the same as specified in the path. Also, remember to add required: true, because path parameters are always required. Here is an example for GET /users/ {id} : name: id # Note the name is the same as in the path

How to set default values for Swagger UI and Swagger editor parameters?

Note for Swagger UI and Swagger Editor users: Parameters with content are supported in Swagger UI 3.23.7+ and Swagger Editor 3.6.34+. Use the default keyword in the parameter schema to specify the default value for an optional parameter.

Is queryparams (model) description reflected in Swagger UI?

it is working as expected but description (added in model) is not reflecting in swagger ui But if same model is used for request body, then description is shown in swagger Am I missing anything to get the description for QueryParams (model) in swagger ui?

How to declare multiple path parameters in fastapi?

You can declare multiple path parameters and query parameters at the same time, FastAPI knows which is which. And you don't have to declare them in any specific order. They will be detected by name: When you declare a default value for non-path parameters (for now, we have only seen query parameters), then it is not required.


2 Answers

This is not possible with Pydantic models

The workaround to get the desired result is to have a custom dependency class (or function) rather than the Pydantic model

from fastapi import Depends, FastAPI, Query

app = FastAPI()


class CustomQueryParams:
    def __init__(
        self,
        foo: str = Query(..., description="Cool Description for foo"),
        bar: str = Query(..., description="Cool Description for bar"),
    ):
        self.foo = foo
        self.bar = bar


@app.get("/test-query/")
async def get_by_query(params: CustomQueryParams = Depends()):
    return params

Thus, you will have the doc as,

screenshot


References

  1. Validate GET parameters in FastAPI--(FastAPI GitHub) It seems like there is less interest in extending the Pydantic model to validate the GET parameters
  2. Classes as Dependencies--(FastAPI Doc)
like image 57
JPG Avatar answered Oct 29 '22 13:10

JPG


This worked for me


from fastapi import Depends, FastAPI, Query

@app.post("/route")
def some_api(
        self,
        query_param_1: float = Query(None, description="description goes here", ),
        query_param_2: float = Query(None, description="Param 2 does xyz"),
):
    
return "hello world"


like image 30
Sanket Berde Avatar answered Oct 29 '22 13:10

Sanket Berde