OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

fastapi swagger docs not rendering list array as query parameter

  • Thread starter Thread starter moth
  • Start date Start date
M

moth

Guest
According to the docs it is possible to pass a list of values https://fastapi.tiangolo.com/tutori...dations/#query-parameter-list-multiple-values .

I have tried on my end and the endpoint http://localhost:8000/items/?q=foo&q=bar correctly process the values as a list:

Code:
{"q":["foo","bar"]}

But the docs do not, and interpret it as a string and do not render multiple options:

enter image description here

Here is my main.py:

Code:
from db import engine,get_db
from models import Example,ExampleModel,Base,Color,Item
from fastapi import FastAPI,Depends,Query,Path
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from contextlib import asynccontextmanager
from typing import Annotated,Union,List
from fastapi.responses import HTMLResponse


@asynccontextmanager
async def lifespan(app: FastAPI):
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    yield

app = FastAPI(lifespan=lifespan) # type: ignore


@app.get("/items/")
async def read_items(q: Annotated[List[str] | None, Query()] = None):
    query_items = {"q": q}
    return query_items
<p>According to the docs it is possible to pass a list of values <a href="https://fastapi.tiangolo.com/tutori...dations/#query-parameter-list-multiple-values" rel="noreferrer">https://fastapi.tiangolo.com/tutori...dations/#query-parameter-list-multiple-values</a> .</p>
<p>I have tried on my end and the endpoint http://localhost:8000/items/?q=foo&q=bar correctly process the values as a list:</p>
<pre><code>{"q":["foo","bar"]}
</code></pre>
<p>But the docs do not, and interpret it as a string and do not render multiple options:</p>
<p><a href="https://i.sstatic.net/FzWG9.png" rel="noreferrer"><img src="https://i.sstatic.net/FzWG9.png" alt="enter image description here" /></a></p>
<p>Here is my <code>main.py</code>:</p>
<pre><code>from db import engine,get_db
from models import Example,ExampleModel,Base,Color,Item
from fastapi import FastAPI,Depends,Query,Path
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from contextlib import asynccontextmanager
from typing import Annotated,Union,List
from fastapi.responses import HTMLResponse


@asynccontextmanager
async def lifespan(app: FastAPI):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield

app = FastAPI(lifespan=lifespan) # type: ignore


@app.get("/items/")
async def read_items(q: Annotated[List[str] | None, Query()] = None):
query_items = {"q": q}
return query_items
</code></pre>
 

Latest posts

Top