feat(litestar): add support for guards with ASGIConnection parameter#700
Open
arynyklas wants to merge 7 commits intoreagento:developfrom
Open
feat(litestar): add support for guards with ASGIConnection parameter#700arynyklas wants to merge 7 commits intoreagento:developfrom
arynyklas wants to merge 7 commits intoreagento:developfrom
Conversation
Add support for injecting ASGIConnection as a parameter type, enabling more flexible handler signatures including guards. Add proper overload signatures to support guard dependencies with correct type inference. Refactor internal injection logic to dynamically detect the connection parameter type instead of hardcoding 'request' or 'socket'. Introduce _build_container_getter to properly resolve container from args/kwargs using function signature binding.
Tishka17
reviewed
Mar 25, 2026
Revert the dynamic parameter detection approach in favor of explicit
inject functions for different handler types.
Add `inject_asgi` function specifically for Litestar guards that
receive `connection: ASGIConnection` as their first parameter. This
provides a clean API for guard dependency injection:
@inject_asgi
async def my_guard(
connection: ASGIConnection,
_: BaseRouteHandler,
config: FromDishka[Config],
) -> None: ...
Fixes the KeyError when using @Inject on guard functions that don't
have 'request' in their kwargs.
Add test coverage for the new inject_asgi decorator used with Litestar guards. Tests verify that: - @inject_asgi properly injects dependencies into guard functions - Dependencies work with ASGIConnection parameter type - Both manual and DishkaRouter auto-injection modes work Conflicts resolved in docs/integrations/litestar.rst
Fix Guard import path from litestar.guards to litestar.types. Remove auto-injection test case as guards require explicit @inject_asgi decorator.
Author
|
@Tishka17, ready for review with tests, coverage |
|
Tishka17
reviewed
Mar 25, 2026
| kind=Parameter.KEYWORD_ONLY, | ||
| )] | ||
|
|
||
| if param_name == "connection": |
Member
There was a problem hiding this comment.
I do not see why user won't put param called connection at any other position in normal handler
Tishka17
reviewed
Mar 30, 2026
| from litestar import ASGIConnection, BaseRouteHandler | ||
| from dishka.integrations.litestar import FromDishka, inject_asgi | ||
|
|
||
| @inject_asgi |
Member
There was a problem hiding this comment.
Can it be used somewhere else? inject_asgi sounds like something more generic than guards only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #699
Adds ASGIConnection parameter support for Litestar guards. Guards receive 'connection' instead of 'request', causing KeyError when using @Inject. Refactors container getter to support both positional and keyword args.