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
get_model_definitions
fails to retrieve already-registered dataclasses after converting them to pydantic dataclasses
#5138
Comments
Kudo's on the comprehensive report and research! (Y) I did some digging as well as to your last question (where is this is happening) and I am very confused. The response_fields are created upon app startup (when However, when the application is being called (for example, because you are going to /docs), it is running at some point I am wrecking my brain on how that is possible... |
Thanks for the explanation. After filing this issue, I took a look at FastAPI's code once more, and even though I claimed FastAPI wasn't caching the converted dataclasses, it seems to be caching them. So, yeah, I'm confused as well. |
Another person on SO has a simpler form to test this issue: import dataclasses
from fastapi import FastAPI
app = FastAPI()
@dataclasses.dataclass
class Response1:
yo: str
@app.post('/one', response_model=Response1)
def get_responses():
pass
@app.post('/two', response_model=Response1) # When I remove this "Response", or I create a second class it works.
def send_responses():
pass
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) Stacktrace: Traceback (most recent call last):
File "/<FOLDER>.venv/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 401, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/applications.py", line 269, in __call__
await super().__call__(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/exceptions.py", line 93, in __call__
raise exc
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/exceptions.py", line 82, in __call__
await self.app(scope, receive, sender)
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/routing.py", line 670, in __call__
await route.handle(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/routing.py", line 266, in handle
await self.app(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/routing.py", line 65, in app
response = await func(request)
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/applications.py", line 224, in openapi
return JSONResponse(self.openapi())
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/applications.py", line 199, in openapi
self.openapi_schema = get_openapi(
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/openapi/utils.py", line 414, in get_openapi
definitions = get_model_definitions(
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/utils.py", line 39, in get_model_definitions
model_name = model_name_map[model]
KeyError: <class 'pydantic.dataclasses.Response1'> |
Any progress on this issue? I've noticed the behavior with |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
http://localhost:8000/docs
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
3.8.13
Additional Context
This bug happens because whatever is converting bare dataclasses to pydantic dataclasses isn't caching the output of the conversion, which means that, in dataclass
B
, the dataclassA
referenced in fielda
is different to the target of the mapping in fieldb
.In a naïve converter, this is what happens:
I don't think this is a bug in pydantic, though, as FastAPI is the library that claims dataclasses are supported.
I could try contributing a PR, if someone could point me to where the pydantic dataclass is generated.
As an additional note, if I don't fill the
responses
dict, this bug doesn't happen.The text was updated successfully, but these errors were encountered: