Request parameters model not exported to docs (and OpenAPI Specification) #11543
Answered
by
YuriiMotov
Philipp3211
asked this question in
Questions
-
First Check
Commit to Help
Example Codefrom typing import Annotated
from fastapi import FastAPI, Depends
from pydantic import BaseModel
class ParamsModel(BaseModel):
foo: str
bar: str
app = FastAPI()
@app.get("/")
def test_route(params: Annotated[ParamsModel, Depends()]):
return params.foo + params.bar DescriptionStart the app then open the browser and go to What is currently:
What I'm expecting:
Why do I need this? I need foo and bar as parameters because on UI side I use caching that depends on foo and bar being present in the URL. Is there a way to get this behavior with FastAPI? Operating SystemLinux, Windows Operating System DetailsNo response FastAPI Version0.110.2 Pydantic Version1.10.2 Python VersionPython 3.9.5 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Answered by
YuriiMotov
May 8, 2024
Replies: 1 comment 13 replies
-
Why do you need the use of from fastapi import FastAPI
from pydantic import BaseModel
class ParamsModel(BaseModel):
foo: str
bar: str
app = FastAPI()
@app.post("/")
def test_route(params: ParamsModel):
return params.foo + params.bar |
Beta Was this translation helpful? Give feedback.
13 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, now I see.. Sorry for the inattention.
I don't think you can easily do what you want in current version of FastAPI since it only accepts simple types (and arrays of simple type parameters) as a Query parameters.
When you use annotation like
params: Annotated[ParamsModel, Depends()]
it is just a shortcut to
params: Annotated[ParamsModel, Depends(ParamsModel)]
FastAPI just looks at the parameters of ParamsModel's init method and treats them as subdependencies. So, it's equivalent of