Skip to content

websocket_route does not work like I expect it should #2513

@jmgurney

Description

@jmgurney

First check

  • [X ] I added a very descriptive title to this issue.
  • [ X] I used the GitHub search to find a similar issue and didn't find it.
  • [ X] I searched the FastAPI documentation, with the integrated search.
  • [ X] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [ X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [ X] I already checked if it is not related to FastAPI but to Pydantic.
  • [ X] I already checked if it is not related to FastAPI but to Swagger UI.
  • [ X] I already checked if it is not related to FastAPI but to ReDoc.
  • [ X] After submitting this, I commit to:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here's a self-contained minimal, reproducible, example with my use case:

from fastapi import FastAPI
from fastapi.testclient import TestClient
from fastapi.websockets import WebSocket

app = FastAPI()


@app.websocket_route("/ws/{arg}/something")
async def websocket(arg, websocket: WebSocket):
    await websocket.accept()
    await websocket.send_json({"msg": "Hello WebSocket"})
    await websocket.close()


def test_websocket():
    client = TestClient(app)
    with client.websocket_connect("/ws/this/something") as websocket:
        data = websocket.receive_json()
        assert data == {"msg": "Hello WebSocket"}

test_websocket()

Description

I copied and slightly modfied the example from https://fastapi.tiangolo.com/advanced/testing-websockets/ .

The issue is that fastapi.routing.APIRouter inherits from starlette.routing.Router, and so websocket_route is inherited from there, and it does not do dependency injection which is quite confusing. The APIRouter should either remove that method, clearly document it or override it so that a consistent interface is provided.

I will commit to creating PR to resolve the issue once I have received advice on how the developers want to resolve this issue. [too much time has passed]

The solution you would like

Fix up the example to use websocket instead of websocket_route (for people using a version of FastAPI that does not yet have the fix). Add websocket_route as an alias for websocket so that starlette's version does not get used.

Describe alternatives you've considered

As documented, I am now using websocket instead of websocket_route, but the project could be improved by making the changes above.

Environment

  • OS: macOSX
  • FastAPI Version: 0.62.0
  • Python version: 3.8.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions