Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger KeyError when two classes contain inner classes with the same name #1505

Closed
tanyaofei opened this issue Jun 3, 2020 · 6 comments
Closed
Labels
question Question or problem question-migrate

Comments

@tanyaofei
Copy link

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
@tanyaofei tanyaofei added the bug Something isn't working label Jun 3, 2020
@tanyaofei tanyaofei changed the title [BUG] Swagger inner class Error [BUG] Swagger KeyError when two classes contain inner classes with the same name Jun 3, 2020
@tiangolo
Copy link
Owner

tiangolo commented Jun 7, 2020

Why would you create a Pydantic class inside of other Pydantic class?

I think that's not really supported by Pydantic...

But yeah, it wouldn't be supported by FastAPI, as there wouldn't be a clean way to get unique IDs for the models.

@tiangolo tiangolo added answered question Question or problem and removed bug Something isn't working labels Jun 7, 2020
@github-actions
Copy link
Contributor

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

@vbyrd
Copy link

vbyrd commented Dec 9, 2020

I suppose this would matter when using modules that return customized pydantic models. I am playing with the pydantic_jsonapi module with FastAPI and encountering the same error. It uses GenericModel from Pydantic to accomplish a jsonapi model. Interestingly, this doesn't seem to happen with the same models used as "response_model" in the decorator and it works fine outside of Fast API.

File "/usr/local/lib/python3.8/site-packages/fastapi/utils.py", line 64, in get_model_definitions
     model_name = model_name_map[model]
KeyError: <class 'pydantic_jsonapi.request.RequestDataModel[typing.Literal['vendor'], VendorCreateModel]'>

https://pydantic-docs.helpmanual.io/usage/models/#generic-models
https://github.com/DeanWay/pydantic-jsonapi

@sureshjoshi
Copy link

sureshjoshi commented Jan 11, 2021

I just ran into this today. In terms of "Why would you create a Pydantic class inside of other Pydantic class" - in my case, the problem happened with two Schemas that had the same inner enum class (not Pydantic inner classes). The reason for it is that the purpose of the type is semantically the same, while happening to have different values.

I can't seem to get string literals to show up in OpenAPI documentation, otherwise I wouldn't use the enums.

Of note: Not a big deal, as there are many workarounds - I just wish the call stack was a bit more descriptive in terms of which object threw the exception, rather than just the enum key :)

@guissart
Copy link

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

@mike0sv
Copy link

mike0sv commented 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.

@tiangolo tiangolo changed the title [BUG] Swagger KeyError when two classes contain inner classes with the same name Swagger KeyError when two classes contain inner classes with the same name Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions github-actions bot removed the answered label Feb 28, 2023
Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7455 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

6 participants