When making an app that uses dependency injection with a list field, the parameter automatically goes to the request body in SwaggerUI:
from fastapi import FastAPI, Query, Depends
import uvicorn
from pydantic import BaseModel, Field
from typing import List
class QueryParams(BaseModel):
name: str = Field(...)
ages: List[int] = Field([])
app = FastAPI()
@app.get("/test")
def test(query: QueryParams = Depends()):
return "hi"
uvicorn.run(app)
Which means I cant test it in swagger UI. Even if I change field to query, it still doesn't work:
from fastapi import FastAPI, Query, Depends
import uvicorn
from pydantic import BaseModel, Field
from typing import List
class QueryParams(BaseModel):
name: str = Field(...)
ages: List[int] = Query([]) # <-- Query
app = FastAPI()
@app.get("/test")
def test(query: QueryParams = Depends()):
return "hi"
uvicorn.run(app)
If I put it in the route function, it works:
from fastapi import FastAPI, Query, Depends
import uvicorn
from pydantic import BaseModel, Field
from typing import List
class QueryParams(BaseModel):
name: str = Field(...)
app = FastAPI()
@app.get("/test")
def test(query: QueryParams = Depends(), ages: List[int] = Query([])):
return "hi"
uvicorn.run(app)
How can I get swagger UI to recognize a list query field in a basemodel with dependency injection?