Thanks to visit codestin.com
Credit goes to github.com

Skip to content

openapi.json throws error when generated with nested models #3782

@yeus

Description

@yeus

First 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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions