Skip to content

OpenAPI generation failed with internal enums and with pydantic >=1.6.*  #2457

@arxell

Description

@arxell

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.

Example

from fastapi import FastAPI
from pydantic import BaseModel
from enum import Enum

app = FastAPI()


class EnumExample(Enum):
    a = 1


class Foo(BaseModel):
    class EnumExample(Enum):
        a = EnumExample.a
        b = 2

    x: EnumExample


class Bar(BaseModel):
    class EnumExample(Enum):
        a = EnumExample.a
        b = 2

    x: EnumExample


class TestResp(BaseModel):
    foo: Foo
    bar: Bar


@app.get("/", response_model=TestResp)
async def test():
    return TestResp(foo=Foo(x=Foo.EnumExample.a), bar=Bar(x=Bar.EnumExample.b))

Description

  • Open the browser and call the endpoint http://127.0.0.1:8000/docs.
  • It returns an error Internal Server Error /openapi.json .
  • The problem is relevant only with pydantic>=1.6.* versions

StackTrace

  File "/Users/arxell/repo/mdcm/alfa/venv39/lib/python3.9/site-packages/fastapi/applications.py", line 143, in openapi
    return JSONResponse(self.openapi())
  File "/Users/arxell/repo/mdcm/alfa/venv39/lib/python3.9/site-packages/fastapi/applications.py", line 121, in openapi
    self.openapi_schema = get_openapi(
  File "/Users/arxell/repo/mdcm/alfa/venv39/lib/python3.9/site-packages/fastapi/openapi/utils.py", line 348, in get_openapi
    definitions = get_model_definitions(
  File "/Users/arxell/repo/mdcm/alfa/venv39/lib/python3.9/site-packages/fastapi/utils.py", line 31, in get_model_definitions
    model_name = model_name_map[model]
KeyError: <enum 'EnumExample'>

Environment

  • OS: macOS
  • FastAPI: 0.62.0
  • Python: 3.9.0
  • pydantic: 1.7.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions