fastapi: openapi.json fails to be generated for nested models
Describe the bug Fails to autogenerate docs.
To Reproduce
- Create a response model with other nested models and try to generate openapi.json or open autogenerated docs.
- Application throws an error
Expected behavior Generate openapi.json and open autogenerated docs without problems.
Environment:
- OS: macOS
- FastAPI Version: 0.30.0
- Python version: 3.7.3
Additional context
My models look like this:
class PaginatedItems(Generic[Item], BaseModel, abc.ABC):
items: List[Any]
has_after: bool = False
has_before: bool = False
class OrderUser(BaseModel):
name: Optional[str] = None
email: EmailStr
class Order(BaseModel):
order_id: UUID
amount: Decimal
currency: str
# Users part
merchants: List[OrderUser]
handlers: List[OrderUser]
class PaginatedOrders(PaginatedItems[Order]):
items: List[Order] = list()
My route definition looks like that:
@router.get(orders_uri,
response_model=PaginatedOrders
async def list():
...
This results in the following exception when trying to open autogenerated docs:
pydantic.error_wrappers.ValidationError: 4 validation errors
schemas -> OrderUser
value is not a valid dict (type=type_error.dict)
schemas -> OrderUser
value is not a valid dict (type=type_error.dict)
components -> schemas
value is not none (type=type_error.none.allowed)
components
value is not none (type=type_error.none.allowed)
When I comment out the response_model=PaginatedOrders part from route definition everything works, but docs obviously miss response type.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (4 by maintainers)
Commits related to this issue
- Fixed pydantic issue on API docs Bug seems to be fixed in fastapi version 0.33: https://github.com/tiangolo/fastapi/issues/383 — committed to bcgov-c/wally by dainetrinidad 4 years ago
oups sorry I think your mistake is putting
response_model=SimilarProducts,in the wrong spot, it’s in the @router partI can confirm it now works fine! Thanks for fixing it so quickly!
@tiangolo yes, I’m happy to say I’ve removed the above workaround from my projects. Thanks!
@yeus I happened to stumble upon it by accident and reposted 👍
I have similar code structure to @yeus and also am running into this issue. I only have issues with
response_modelvalues that have nested iterable types (i.e.List,Tuple,Dict). I did some additional testing that made me think this was a Windows vs Linux issue, but upon further investigation I found that something became broken in the most recent version of fastapi.This code will generate proper documentation when using
fastapi[standard]==0.65.2but does not generate proper documentation infastapi[standard]==0.68.1.When the above is run using
fastapi[standard]==0.65.2, I can access documentation at127.0.0.1:8001/docs.When the above is run using
fastapi[standard]==0.68.1, I receive the following error:For now, I am just downgrading fastapi so that documentation works.
I feel like reopening this bug. I get the same erros as above by using a nested model like this:
Dict[str, List[Tuple[str, float]]]I am on: fastapi version: ‘0.68.1’, python 3.8.10, ubuntu 20.04.
this code here:
causes the same error to appear.