diff --git a/fastapi/routing.py b/fastapi/routing.py index 1ac4b3880..233f79fcb 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -297,14 +297,14 @@ class APIWebSocketRoute(routing.WebSocketRoute): self.path = path self.endpoint = endpoint self.name = get_name(endpoint) if name is None else name - self.dependant = get_dependant(path=path, call=self.endpoint) + self.path_regex, self.path_format, self.param_convertors = compile_path(path) + self.dependant = get_dependant(path=self.path_format, call=self.endpoint) self.app = websocket_session( get_websocket_app( dependant=self.dependant, dependency_overrides_provider=dependency_overrides_provider, ) ) - self.path_regex, self.path_format, self.param_convertors = compile_path(path) def matches(self, scope: Scope) -> Tuple[Match, Scope]: match, child_scope = super().matches(scope) diff --git a/tests/test_ws_router.py b/tests/test_ws_router.py index fbca104a2..206d743ba 100644 --- a/tests/test_ws_router.py +++ b/tests/test_ws_router.py @@ -35,6 +35,14 @@ async def routerindex2(websocket: WebSocket): await websocket.close() +@router.websocket("/router/{pathparam:path}") +async def routerindexparams(websocket: WebSocket, pathparam: str, queryparam: str): + await websocket.accept() + await websocket.send_text(pathparam) + await websocket.send_text(queryparam) + await websocket.close() + + async def ws_dependency(): return "Socket Dependency" @@ -106,3 +114,14 @@ def test_router_ws_depends_with_override(): app.dependency_overrides[ws_dependency] = lambda: "Override" with client.websocket_connect("/router-ws-depends/") as websocket: assert websocket.receive_text() == "Override" + + +def test_router_with_params(): + client = TestClient(app) + with client.websocket_connect( + "/router/path/to/file?queryparam=a_query_param" + ) as websocket: + data = websocket.receive_text() + assert data == "path/to/file" + data = websocket.receive_text() + assert data == "a_query_param"