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

Injection does not work with Fastapi websocket endpoints #40

Open
mehmetgiritli opened this issue Nov 29, 2024 · 4 comments
Open

Injection does not work with Fastapi websocket endpoints #40

mehmetgiritli opened this issue Nov 29, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@mehmetgiritli
Copy link

With FastAPI, trying to inject to into a websocket endpoint function fails to resolve the dependency.

Consider this:

@app.websocket("/progress")
async def websocket_endpoint(websocket: WebSocket, progress: Annotated[Progress, Inject()]):
    await websocket.accept()
    await progress.get(websocket)

Returns:

    await progress.get(websocket)
          ^^^^^^^^^^^^
AttributeError: 'EmptyContainerInjectionRequest' object has no attribute 'get'

Note the object type EmptyContainerInjectionRequest.

Injecting the same into a endpoint function decorated with @app.getworks perfectly fine.

@maldoinc
Copy link
Owner

maldoinc commented Nov 29, 2024

The integration currently does not autowire websocket endpoints but that should be an easy fix.

See #41.

If you can try it out and give some feedback by installing it from pip install git+https://github.com/maldoinc/wireup.git@fastapi/websocket that'd be great as I haven't tested it extensively yet.

@maldoinc maldoinc added the enhancement New feature or request label Nov 29, 2024
@mehmetgiritli
Copy link
Author

Thank you, it works fine, however I do have a related suggestion. Websocket itself is not injectable, would it be possible to make it so? In a way, similar to how FastAPI Request is handled...

@maldoinc
Copy link
Owner

I released v0.14.0 which allows injection in websocket routes. Exposing WebSocket like Request will come in a later release, for now you can just forward the connection or any data from it from the websocket endpoint.

@mehmetgiritli
Copy link
Author

Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants