Skip to content

Fixes Register Endpoint for Router of Instance of APIWebSocketRoute - #358

Closed
HideyoshiNakazone wants to merge 4 commits into
fastapiutils:masterfrom
HideyoshiNakazone:fixes-websocket-implementation
Closed

HideyoshiNakazone wants to merge 4 commits into
fastapiutils:masterfrom
HideyoshiNakazone:fixes-websocket-implementation

Conversation

@HideyoshiNakazone

@HideyoshiNakazone HideyoshiNakazone commented Feb 3, 2025 •

Copy link
Copy Markdown

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
Copy Markdown
Author

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

Thanks a lot,
Vitor Hideyoshi.

@yuval9313 yuval9313 left a comment

Copy link
Copy Markdown
Collaborator

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

Comment thread fastapi_utils/cbv.py Outdated
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
Copy Markdown
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
Copy Markdown
Author

Choose a reason for hiding this comment

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