Issue 1:
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:

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 2:
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 fastapi import Depends, FastAPI, Request
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseSettings

class Settings(BaseSettings):
token_url: str

def get_settings() -> Settings:
return Settings()

app = FastAPI()

def get_oauth2_scheme(settings=Depends(get_settings)):
return OAuth2PasswordBearer(tokenUrl=settings.token_url)

async def use_token(request: Request, oauth2_scheme=Depends(get_oauth2_scheme)):
return await oauth2_scheme(request)

@app.get("/items/")
async def read_items(token: str = Depends(use_token)):
return {"token": token}

Description

Run saved script, for example: TOKEN_URL="token" uvicorn tmp:app

Navigate to http://127.0.0.1:8000/docs

Notice there is no "Authorize" button in docs

Endpoint is protected, a request fails with a 401 error

Wanted Solution

The Dependency Injection system doesn't seem to be fully resolving these dependencies so it can auto-detect the authentication mechanism. It seems to only resolve first surface level dependencies. This seems to apply to any Depends()

objects for authorization, either in route definitions or route signatures.

Wanted Code

(The example/reproduction code working would be great, no extra syntax needed)

Alternatives

No response

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.37.0

Python Version

3.9

Additional Context

Discussion in Discord here: https://discord.com/channels/772607987558252564/772631203689922562/942107142859673732

https://cdn.discordapp.com/attachments/772631203689922562/942107232315777094/unknown.png