-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
🐛 Fix using Annotated
in routers or path operations decorated multiple times
#9315
🐛 Fix using Annotated
in routers or path operations decorated multiple times
#9315
Conversation
📝 Docs preview for commit 49c965c at: https://642030d2e2f56d1fc2f7a3e6--fastapi.netlify.app |
49c965c
to
c21ac1f
Compare
We need to copy the field_info to prevent ourselves from mutating it. This allows multiple path or nested routers ,etc.
c21ac1f
to
069e485
Compare
tests/test_route_scope.py
Outdated
@@ -47,4 +47,4 @@ def test_websocket(): | |||
def test_websocket_invalid_path_doesnt_match(): | |||
with pytest.raises(WebSocketDisconnect): | |||
with client.websocket_connect("/itemsx/portal-gun"): | |||
pass | |||
pass # pragma: nocover |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, locally I need this to have 100% coverage
I can revert this to keep the PR cleaner if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass
never gets executed. I've found the same thing before.
Might client.websocket_connect("/itemsx/portal-gun")
work, instead?
📝 Docs preview for commit 069e485 at: https://64213563bf51d1544b1883b2--fastapi.netlify.app |
I ran into this issue today so decided to give this PR a test drive. Seems to solve the issue nicely. Thanks @sharonyogev! |
This PR also fixed this issue on one of my projects ! |
Same here: This fixed the issue I had with
... although set up as @router.get("/", ...)
async def some_function(
some_param: Annotated[
list[str],
Query(description="This is some parameter"),
] = []
) # ... |
Please have it merge ASAP |
This solved my issue, great work! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏻
As stated in the discussion (actually a real issue), this fix solves my problem |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This fixes a bug I introduced in #4871.
This will fix my issue as well! Any idea when this can get merged in? |
A monkey patch workaround for until this gets merged: fastapi_analyze_param = fastapi.dependencies.utils.analyze_param
def fastapi_analyze_param_monkey_patch(*, annotation, **kwargs):
"""
Workaround for until fastapi!9315 is merged.
This needs to be patched in before you import/call any APIRouters
"""
from typing import Annotated, get_args, get_origin
from copy import copy
if get_origin(annotation) is Annotated:
annotation = Annotated[tuple(copy(arg) for arg in get_args(annotation))]
return fastapi_analyze_param(annotation = annotation, **kwargs)
fastapi.dependencies.utils.analyze_param = fastapi_analyze_param_monkey_patch |
Co-authored-by: Nadav Zingerman <7372858+nzig@users.noreply.github.com>
Annotated
in routers or path operations decorated multiple times
📝 Docs preview for commit e11eb50 at: https://643840014d5e810cf59c606f--fastapi.netlify.app |
Awesome, great job @sharonyogev! 🙇 Thanks everyone for the comments and for testing it locally to confirm that it solved it for you. 👏 This will be released in the next hour or so in FastAPI 0.95.1 🎉 |
Thanks @tiangolo, glad I could help 😃 |
We need to deep clone the field_info to prevent ourself from mutating it. This allows multiple path or nested routers ,etc.
Should solve those discussions:
#9279
#9309
#9306