question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Swagger KeyError when two classes contain inner classes with the same name

See original GitHub issue

Describe 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:

  1. 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)
  1. Open the browser and call the endpoint /docs.
  2. 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
mike0svcommented, Aug 20, 2021

I generate endpoints dynamically based on some config, and generate models for those endpoints with pydantic.create_model. Code in get_model_name_map silently drops models with identical names, which leads to uninformative KeyError later.

3reactions
guissartcommented, Jan 22, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found