-
-
Notifications
You must be signed in to change notification settings - Fork 343
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
Bug: query parameters can also be picked up from constructors of injected classes #2470
Comments
This is intended behaviour and generally how the dependency injection works, it's not unique to classes. Any callable can request parameters. If this were not the case, you couldn't really use any parameters in dependencies, as you'd have to explicitly request them in the handler and pass them to a dependency callable, greatly reducing their usefulness. I'd suggest that you keep your dependency chain / other utility functions separated to avoid cases like these. |
😱 so you can have qParams, hidden away in dependencies? This seems like a dangerous thing. |
I think it actually makes sense if you think of query parameters as implicitly defined dependencies. You can request dependencies within dependencies without explicitly declaring them, so query parameters shouldn't be an exception to that. That being said, we have previously talked about getting rid of the implicit requests for anything other than dependencies in favour of a more explicit approach. def some_dependency(another_query_param: Query[str]) -> str:
return another_query_param
@get("/{path_param:str}", dependencies={"some": some_dependency})
async def handler(
path_param: str,
some: str,
data: Body[dict[str, str]],
query_param: Query[int],
header_param: Header[str],
) -> str:
... where everything that's not directly injected into the handler has to be explicitly declared. Maybe it's worth bringing this up again @litestar-org/maintainers? |
I like the idea, and it would allow us to get rid of reserved kwargs, e.g., users could name "data" whatever they want (recent relevant discord discussion: https://discord.com/channels/919193495116337154/1164442955357110292). |
Description
after this our swagger showed:
I would assume, qParams (and such) would only be picked u in the signature of the method in the controller?
URL to code causing the issue
No response
MCVE
No response
Steps to reproduce
No response
Screenshots
No response
Logs
No response
Litestar Version
2.2.1
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh Litestar dashboard
The text was updated successfully, but these errors were encountered: