Skip to content
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: Arbitrary dependencies in WebSocketListener fail #1762

Closed
provinzkraut opened this issue May 30, 2023 · 0 comments · Fixed by #1807
Closed

Bug: Arbitrary dependencies in WebSocketListener fail #1762

provinzkraut opened this issue May 30, 2023 · 0 comments · Fixed by #1807
Assignees
Labels
Bug 🐛 This is something that is not working as expected

Comments

@provinzkraut
Copy link
Member

provinzkraut commented May 30, 2023

Description

Injecting arbitrary dependencies (that is, dependencies that are not "reserved" like state) cannot be successfully injected into a WebSocketListener subclass. The injection works as intended when using the websocket_listener decorator.

MCVE

from litestar import WebSocket, Litestar
from litestar.handlers import WebsocketListener
from litestar.testing import TestClient


class Listener(WebsocketListener):
    path = "/ws"

    def on_accept(self, socket: WebSocket, foo: str) -> None:
        pass

    async def on_receive(self, data: dict) -> dict:
        return data


app = Litestar(
    [Listener],
    dependencies={"foo": lambda: "bar"},
)


with TestClient(app) as client, client.websocket_connect("/ws") as ws:
    ws.send_json({"foo": "bar"})

Litestar Version

2.0.0alpha7

@provinzkraut provinzkraut added Bug 🐛 This is something that is not working as expected Triage Required 🏥 This requires triage and removed Triage Required 🏥 This requires triage labels May 30, 2023
@provinzkraut provinzkraut self-assigned this Jun 10, 2023
provinzkraut added a commit that referenced this issue Jun 13, 2023
* fix(1762): resolves issue with custom dep injection on WebsocketListener

* Update tests/handlers/websocket/test_listeners.py

Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Update tests/handlers/websocket/test_listeners.py

Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Update litestar/handlers/websocket_handlers/listener.py

Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Update litestar/handlers/websocket_handlers/listener.py

Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Update tests/handlers/websocket/test_listeners.py

Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>

* Add back overrides

---------

Co-authored-by: Janek Nouvertné <25355197+provinzkraut@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant