Conversation
src/dishka/integrations/litestar.py
Outdated
| await next_app(scope, receive, send) | ||
|
|
||
|
|
||
| class DishkaPlugin(InitPlugin): |
There was a problem hiding this comment.
can we add plugin automatically inside setup_dishka?
There was a problem hiding this comment.
I dont think so,
the current api is
app = litestar.Litestar()
container = make_async_container(provider)
setup_dishka(container, app)
and this PR just adds the possibility to do:
container = make_async_container(provider)
app = litestar.Litestar(plugins=[DishkaPlugin(container=container)])
I dont know a litestar's built-in way to add a plugin other than using the plugins kwarg unfortunately.
|
@euri10 please update to fix conflicts |
|
sorry back on the drawing board i didnt notice there were that many changes so I have to adapt :) |
|
Tests failed: |
|
ASGIMiddleware appeared in 2.15 so indeed this is not in 2.3.2
Is there is a way with nox to skip tests ? but to be fair I never used it, feel free to correct it. |
|
We cannot drop support for existing users. You can hide logic under try/except if you find this useful. You can also use pytest features for skipping new tests |
|
To merge this we need:
|
def di_middleware_factory(app: ASGIApp) -> ASGIApp:
async def dishka_middleware(scope: Scope, receive: Receive, send: Send, next_app: ASGIApp = app) -> None:
if scope["type"] == ScopeType.HTTP:
request: Request = Request(scope=scope, receive=receive, send=send)
async with request.app.state.dishka_container(
context={Request: request},
scope=DIScope.REQUEST,
) as request_container:
request.state.dishka_container = request_container
await next_app(scope, receive, send)
elif scope["type"] == ScopeType.WEBSOCKET:
websocket: WebSocket = WebSocket(scope=scope, receive=receive, send=send)
async with websocket.app.state.dishka_container(
context={WebSocket: websocket},
scope=DIScope.SESSION,
) as request_container:
websocket.state.dishka_container = request_container
await next_app(scope, receive, send)
else:
await next_app(scope, receive, send)
return dishka_middlewareuse |
Can you, please, update documentation? |
|
Sorry to bother, but I do not still get how plugins can help to specify an order of middlewares? User can want to access cotnainer in middleware, though you we right about handling container errors |
|
let's formalize expectations:
Can we write scenarios and expected behavior in more details? |
|
1st of all sorry for the slow responsiveness, I'm afraid I'll be less available on this in the near future, this said I'll try my best.
This is clearly an oversight on our part and it should have been included in the changelog when releasing 2.15.0 Litestar currently provides you with 2 middleware abstractions, you are using currently none of them:
you can read more on this here https://docs.litestar.dev/latest/usage/middleware/creating-middleware.html#migrating-from-middlewareprotocol-abstractmiddleware Like said in our docs, a middleware is any callable so your
So this is the trade-off of using a callable rather than extending our base class, both work, using the built-ins gives you a little bit more configurability in a sense, but it's fine doing it the way you do.
you know this of course, the middleware stack is like an onion, usually you want the ExceptionMiddleware raising exceptions to be the outermost one. By "skipping" our middleware building logic you put the callable at the outermost position. The plugin mechanism however will make sure this is not the case, so you'll have it right before the exception one. So plugins dont really help you specify an order (more on that later) but make sure the Exception one is the last one.
So to synthetize things, plugin is imho the only way to make sure the dishka middleware is correctly wrapped in the |
|
Thank you for your explanation. That sounds interesting, but probably has more sense once version 3 is released. So, I'll take a pause and think about options here and how can we coordinate approaches with other intergrations as well. Regarding the exception handling:
Taking this into account, we need to review expectations and possible cases. |
|



the current integration "hacks" litestar's
asgi_handleradding the dishka middleware as the outermost middleware.I'm not too sure if it is correct as you usually want the exception middleware to be the outermost one.
this proposal uses Litestar's plugin system and puts the same dishka_container in the state but adds a middleware to the usual middleware stack so that everything is wrapped correctly .
I adedd tests so that it remains backward compatible