Skip to content

Conversation

HideyoshiNakazone
Copy link

@HideyoshiNakazone HideyoshiNakazone commented Feb 3, 2025

Versions

Python Version: 3.13

Package Versions:

  • fastapi: 0.115.8
  • fastapi-utils: 0.8.0

Bug

When creating a Route of type APIWebSocketRoute the following error is thrown:

Traceback (most recent call last):
  File "/home/user/Downloads/teste/main.py", line 7, in <module>
    @cbv(router)
     ^^^^^^^^^^^
  File "/home/user/.cache/pypoetry/virtualenvs/teste-4HLGbWUH-py3.12/lib/python3.12/site-packages/fastapi_utils/cbv.py", line 46, in decorator
    return _cbv(router, cls, *urls)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.cache/pypoetry/virtualenvs/teste-4HLGbWUH-py3.12/lib/python3.12/site-packages/fastapi_utils/cbv.py", line 57, in _cbv
    _register_endpoints(router, cls, *urls)
  File "/home/user/.cache/pypoetry/virtualenvs/teste-4HLGbWUH-py3.12/lib/python3.12/site-packages/fastapi_utils/cbv.py", line 112, in _register_endpoints
    raise ValueError("The provided routes should be of type APIRoute")
ValueError: The provided routes should be of type APIRoute

This error can be replicated using the following example:

from fastapi import FastAPI, WebSocket, WebSocketDisconnect, APIRouter
from fastapi_utils.cbv import cbv

app = FastAPI()
router = APIRouter()

@cbv(router)
class WebSocketCBV:
    @router.websocket("/ws")
    async def websocket_endpoint(self, websocket: WebSocket):
        await websocket.accept()
        try:
            while True:
                data = await websocket.receive_text()
                await websocket.send_text(f"Message text was: {data}")
        except WebSocketDisconnect:
            print("Client disconnected")

app.include_router(router)

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Solution

The solution i recommend is to just add the class to instance check of the _register_endpoints function and then add it with a fixed route_method of "WS" since the APIWebSocketRoute does not have a methods atribute.

Thank you very much,
Vitor Hideyoshi.

@HideyoshiNakazone
Copy link
Author

Hey @yuval9313, could you or another maintainer take a look in this merge request please?

Thanks a lot,
Vitor Hideyoshi.

Copy link
Collaborator

@yuval9313 yuval9313 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are also missing test case

@@ -108,12 +108,16 @@ def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None:
_allocate_routes_by_method_name(router, url, function_members)
router_roles = []
for route in router.routes:
if not isinstance(route, APIRoute):
raise ValueError("The provided routes should be of type APIRoute")
if not isinstance(route, APIRoute) and not isinstance(route, APIWebSocketRoute):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, this is really nit pick but I prefer not (condition a or condition b)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem at all, i'll happily make the change :)

@HideyoshiNakazone HideyoshiNakazone force-pushed the fixes-websocket-implementation branch from b12a8cd to a224089 Compare February 24, 2025 21:48
@HideyoshiNakazone
Copy link
Author

HideyoshiNakazone commented Feb 24, 2025

You are also missing test case

Hello, I've added a test case in the unittests for the websocket example, it tests the three basic operations on the websocket connection (can accept connection, can send_text, receive_text and can close). I hope this test is okay :)

@HideyoshiNakazone HideyoshiNakazone force-pushed the fixes-websocket-implementation branch from 790d46a to 5e2b161 Compare March 9, 2025 21:03
@kaziiriad
Copy link

#367

@HideyoshiNakazone
Copy link
Author

Hello @yuval9313 , can we resume this MR? I'd love to have this merged

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

Successfully merging this pull request may close these issues.

3 participants