I am finishing up an API in FastAPI and using the OpenAPI documentation with pydantic to generate docs. One thing I’ve noticed, is that the hierarchy of nested models is not preserved when one field is an array of models. The example below is the approximate structure of my model.
Class MyModel(BaseModel):
description: str
success: bool
...
Class TopLevel(BaseModel):
message: str
matches: List[MyModel] = Field(
List[MyModel],
description="An array of matches found",
)
However, in the docs for FastAPI, the matches field simply shows as array<object> and lists the subentries as “Item”
matches Collapse all array<object>
Items Collapse all object
description Expand all(string | null)
success Expand all(object | null)
What am I missing here? The structure is preserved in other nested scenarios when the nested fields are signular objects of another pydantic model. Why is array<object> being generated instead of array<MyModel>.
I’ve tried using TypeVar, list, and changing the annotation of the sub model. No luck in preserving the structure/class name.