New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Using Nested Pydantic models and params: MyModel = Depends()
forces OpenAPI docs GET methods to require a request body.
#11037
Comments
params: MyModel = Depends()
seems to force swagger ui docs to require a request body for GET methods.params: MyModel = Depends()
seems to force OpenAPI docs to require a request body for GET methods.
params: MyModel = Depends()
seems to force OpenAPI docs to require a request body for GET methods.params: MyModel = Depends()
forces OpenAPI docs GET methods to require a request body.
I faced a similar issue, the only way I found to make it work in the way you described was to explicitly pass a class MyModelData1(BaseModel):
archive: Optional[str] = Field(None, description="Archive name")
archive_type: Optional[str] = Field(None, description="Archive type")
class MetadataGet(BaseModel):
id: Optional[str] = Field(None, alias="_id")
foreign_key: Optional[str] = Field(None, alias="_foreign_key")
class DeepNestedModelGet(BaseModel):
name: Optional[str] = None
version: Optional[str] = None
class DetailsModelGet(BaseModel):
some_data: Optional[List[DeepNestedModelGet]] = None
some_data2: Optional[List[Optional[str]]] = None
class MyModelData2(BaseModel):
id: Optional[str] = Field(None, alias="_id")
details: DetailsModelGet = Depends()
meta: MetadataGet = Depends() In this case, the request body is not required for the |
params: MyModel = Depends()
forces OpenAPI docs GET methods to require a request body.params: MyModel = Depends()
forces OpenAPI docs GET methods to require a request body.
I believe using Pydantic models for GET params is not officially supported, and whatever of it works is only the case by accident. Using nested Pydantic models to define GET params is particularly problematic; there are many mentions of this if you search the issues and discussions. See this comment by @tiangolo. The roadmap has plans for official support:
|
That does appear to be the case. Thank you for the links, I had overlooked the comment in the closed discussion when it was marked answered w/ Pydantic v2 support, as well as the Roadmap as it did not explicitly mention |
Since this comes up so often (judging by the number of issues and discussions), maybe it would be good to explicitly mention in the Query Parameters docs that using Pydantic models is not yet supported? |
Hi, if you want to store data in a class, consider using dataclasses with query parameters. For the body, if there are only optional fields and any data will be sent, you should use Depends(). |
First check
Example
Here's a self-contained minimal, reproducible, example with my use case:
Description
/data1/
./data2/
.TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
is returned.The solution you would like
The request body is not a required part of the GET
/data2/
UI documentation when using an identicalDepends()
structure sodump_model()
method can be used to pass very large models to the documentation as query parameters for the GET method which are optional.Environment
Additional context
I've gone so far as to take
List[str]
which becameOptional[List[str]
in the Get version of the model, to an extreme ofOptional[List[Optional[str]]]
andList[DetailsModelGet]
which was already all optional fields intoOptional[List[Optional[DetailsModelGet]]]
. Pushing Optional down to the last type and making "deeply optional" fields in case somehow this would resolve the issue. I cannot seem to find any instance of how to get the nested models not to result in a required request body in the docs GET method except to remove the nested (optional) models entirely and use a single-layer Pydantic model.Originally posted by @stevesuh in #7275
The text was updated successfully, but these errors were encountered: