[BUG] openapi.json throws error when generated with nested models
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from typing import List, Dict, Tuple
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Answers(BaseModel):
answers: Dict[str, List[Tuple[str, float]]] = {}
@app.get("/qa", response_model=Answers)
async def qatask(request: str):
return Answers()
if __name__ == "__main__":
import uvicorn
uvicorn.run("bugfind:app", host="0.0.0.0", port=5000, reload=True,
workers=1) # , debug=True)
Description
open http://localhost:5000/docs or http://localhost:5000/openapi.json and you will see the error in the logs.
Operating System
Linux
Operating System Details
ubuntu 20.04
FastAPI Version
0.68.1
Python Version
3.8.10
Additional Context
There was a bug #383 a while ago reporting the same problem but closed. This new issue here is in order to reopen that bug as it still isn’t solved or reappeared since then. Fell free to close this new issue and reopen the old bug if that is preferred.
this is the error:
Traceback (most recent call last):
File "//home/name/.local/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 373, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "//home/name/.local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "//home/name/.local/lib/python3.8/site-packages/fastapi/applications.py", line 208, in __call__
await super().__call__(scope, receive, send)
File "//home/name/.local/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "//home/name/.local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "//home/name/.local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "//home/name/.local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "//home/name/.local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "//home/name/.local/lib/python3.8/site-packages/starlette/routing.py", line 580, in __call__
await route.handle(scope, receive, send)
File "//home/name/.local/lib/python3.8/site-packages/starlette/routing.py", line 241, in handle
await self.app(scope, receive, send)
File "//home/name/.local/lib/python3.8/site-packages/starlette/routing.py", line 52, in app
response = await func(request)
File "//home/name/.local/lib/python3.8/site-packages/fastapi/applications.py", line 161, in openapi
return JSONResponse(self.openapi())
File "//home/name/.local/lib/python3.8/site-packages/fastapi/applications.py", line 136, in openapi
self.openapi_schema = get_openapi(
File "//home/name/.local/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 410, in get_openapi
return jsonable_encoder(OpenAPI(**output), by_alias=True, exclude_none=True) # type: ignore
File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 4 validation errors for OpenAPI
components -> schemas -> Answers -> properties -> answers -> additionalProperties -> items -> items
value is not a valid dict (type=type_error.dict)
components -> schemas -> Answers -> properties -> answers -> additionalProperties -> $ref
field required (type=value_error.missing)
components -> schemas -> Answers -> properties -> answers -> additionalProperties
value could not be parsed to a boolean (type=type_error.bool)
components -> schemas -> Answers -> $ref
field required (type=value_error.missing)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Scalatra Swagger UI Unable To Map $ref For Nested Model
I am getting this error that model not found. This is very strange because the JSON has all the models associated to the...
Read more >Swagger PUT/POST body value issue
It looks to be a real problem, impacting people using swagger JSON based tools such as NSwagStudio to create client APIs with POST/PUT/PATCH ......
Read more >An adventure in OpenAPI V3 code generation | Mux blog
OpenAPI Generator is a comprehensive Java application which can generate client and server side code from your OpenAPI models. It's a large code ......
Read more >Extending OpenAPI - FastAPI - tiangolo
openapi() method that is expected to return the OpenAPI schema. As part of the application object creation, a path operation for /openapi.json (or...
Read more >How to Make the Most of Pydantic - Towards Data Science
Achieve higher interoperability with JSON Schemas. Simplify data model processing with Python's built-in functions. Parsing data the safe way. JSON is the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It seems this is a duplicate of https://github.com/tiangolo/fastapi/issues/3665
In short, it should be fixed by FastAPI
0.73.0
, released in the next hours. 🎉For more info, read the last comment in that issue: https://github.com/tiangolo/fastapi/issues/3665#issuecomment-1019579214
And the comment in the PR: https://github.com/tiangolo/fastapi/pull/3874#issuecomment-1019578028
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.