[Python] FastAPI openapi_examples not working with Query() although it works with Body(). How...

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am using fastapi==0.115.12 and I have the following code:

    from fastapi import FastAPI, Body, Query
    from pydantic import BaseModel

    app = FastAPI()

    class GetFrameworksRequest(BaseModel):
    devtype: str
    languages: Optional[list[str]] | None = None

    @app.get("/v1/swe/lookup/frameworks")
    async def get_frameworks(query: Annotated[GetFrameworksRequest, Body(
    openapi_examples={
    "languages are provided": {
    "value": {
    "devtype": "backend",
    "languages": ["python", "javascript"]
    }
    },
    "languages are not provided": {
    "value": {
    "devtype": "backend",
    "languages": None
    }
    }
    }
    )])


    Which as expected shows the dropdown of examples (and replaces the code in the example as well):

    [​IMG]

    However, if I just change the Body to Query it breaks.

    My question is, how to properly document examples for Query parameters such that I can have the nice documentation showing a dropdown and that has a code replacement when I select an option (just like the Body)? Or am I getting something wrong here?

    Continue reading...

Compartilhe esta Página