[BUG] Swagger KeyError when two classes contain inner classes with the same name
See original GitHub issueDescribe the bug
KeyError when two classes contain inner classes with the same name. If i rename one of the inner classes, app runs good.
To Reproduce
Steps to reproduce the behavior with a minimum self-contained file.
Replace each part with your own scenario:
- Create a file with:
from __future__ import annotations
from typing import Union
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
class X(BaseModel):
class A(BaseModel):
pass
a: X.A
X.update_forward_refs()
class Y(BaseModel):
class A(BaseModel):
pass
y: Y.A
Y.update_forward_refs()
app = FastAPI()
@app.get("/")
def route(request: Union[X, Y]):
return str(request)
- Open the browser and call the endpoint
/docs
. - It show
Fetch error Internal Server Error /openapi.json
and the app raise an error
File "/usr/local/lib/python3.7/site-packages/fastapi/utils.py", line 64, in get_model_definitions
model_name = model_name_map[model]
KeyError: <class '__main__.X.A'>
Environment
- OS: MacOS
- FastAPI Version : 0.55.1
- Python: 3.8.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Swagger different classes in different namespaces with same ...
It will build these schema Ids based on the class names of the objects. When you try to have two or more classes...
Read more >Python KeyError - Javatpoint
It's a built-in exception class raised by several modules that interact with dicts or objects containing key-value pairs. Now, we know that what...
Read more >Handling Validation Errors - python-jsonschema
When an invalid instance is encountered, a ValidationError will be raised or returned, depending on which method or function is used.
Read more >Release Notes — Airflow Documentation
Built-in operator classes that use this dep class (including sensors and all subclasses) already have this attribute and are not affected. The deps...
Read more >Rasa Open Source Change Log
#11207: Update documentation for customizable classes such as ... of the same name, you now have to define a from_entity mapping in 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 Free
Top 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
I generate endpoints dynamically based on some config, and generate models for those endpoints with
pydantic.create_model
. Code inget_model_name_map
silently drops models with identical names, which leads to uninformative KeyError later.@sureshjoshi @tanyaofei reading this issue saved my day.
I had a duplicated Enum class with the same name across two Pydantic model. And the Key error that it produced doesn’t give many clue on what happened.
Naming differently the two Enum class solve the problem.