1

In my FastAPI application I have a number of endpoints, each with about 20 query parameters that I am generating automatically. I am setting the parameters using the openapi_extra option in the endpoint:

@app.get("/COLORS/", openapi_extra=generate_params(schemas.COLORS))
async def colors( request: Request ):
    return handle_request( # stuff that is not relevant #)   

When the FastAPI server is started this will generate something that looks like:

What I would like to do is put the RED_, GREEN_, and BLUE_ parameters into their own little collapsable menus if possible so that the docs will look cleaner. Is this possible and how can I do it?

Jared DuPont
  • 107
  • 1
  • 7
  • Please have a look at related answers [here](https://stackoverflow.com/a/74370808/17865804), as well as [here](https://stackoverflow.com/a/75998823/17865804) and [here](https://stackoverflow.com/a/76131490/17865804) – Chris Jul 18 '23 at 11:48

2 Answers2

0

Modify openapi's "schema" of each parameter in your generate_params function. For reference you can declare Pydantic model, that suits your shape, and inspect its schema (i.e. Color.schema())

Alexander Farkas
  • 529
  • 3
  • 11
  • I am not sure I understand what you mean, can you give an example of a schema that would produce collapsable menus under an endpoint? – Jared DuPont Aug 27 '21 at 16:30
  • @JaredDuPont {'title': 'Help', 'type': 'object', 'properties': {'email': {'title': 'Email', 'type': 'string'}, 'color': {'$ref': '#/definitions/Color'}}, 'required': ['email', 'color'], 'definitions': {'Color': {'title': 'Color', 'description': 'An enumeration.', 'enum': ['RED', 'BLUE']}}} – Alexander Farkas Aug 27 '21 at 16:36
0

Are you looking for adding a choiceable dropdown menu in openapi as query parameters like an existing enum?

If so here's an example:

from fastapi import FastAPI, Query

app = FastAPI()

@app.post("/send-invoice")
async def send_invoice(
    priority: str = Query("normal", enum=["normal", "high"])
) -> dict:
        return {"result": priority}

enter image description here

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150