最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - FastAPI openapi_examples not working with Query() although it works with Body(). How can I make it work with Query()? -

programmeradmin5浏览0评论

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):

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?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论