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

Fixes Register Endpoint for Router of Instance of APIWebSocketRoute #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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)

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.

2 participants