Skip to content

feat(litestar): add support for guards with ASGIConnection parameter#700

Open
arynyklas wants to merge 7 commits intoreagento:developfrom
arynyklas:develop
Open

feat(litestar): add support for guards with ASGIConnection parameter#700
arynyklas wants to merge 7 commits intoreagento:developfrom
arynyklas:develop

Conversation

@arynyklas
Copy link
Copy Markdown

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.

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.
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.
@arynyklas
Copy link
Copy Markdown
Author

@Tishka17, ready for review with tests, coverage

@sonarqubecloud
Copy link
Copy Markdown

kind=Parameter.KEYWORD_ONLY,
)]

if param_name == "connection":
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see why user won't put param called connection at any other position in normal handler

@arynyklas arynyklas requested a review from Tishka17 March 28, 2026 07:35
from litestar import ASGIConnection, BaseRouteHandler
from dishka.integrations.litestar import FromDishka, inject_asgi

@inject_asgi
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be used somewhere else? inject_asgi sounds like something more generic than guards only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

No support for guards in Litestar

2 participants