Skip to content

[BUG] Error When Register Endpoint for Router of Instance of APIWebSocketRoute #359

Description

@HideyoshiNakazone

Describe the bug
When trying to add a @router.websocket endpoint to a CBV Router the following exception is throw:

Traceback (mostrecentcalllast):
File"/home/user/Downloads/teste/main.py", line7, in<module>
@cbv(router)
^^^^^^^^^^^File"/home/user/.cache/pypoetry/virtualenvs/teste-4HLGbWUH-py3.12/lib/python3.12/site-packages/fastapi_utils/cbv.py", line46, indecoratorreturn_cbv(router, cls, *urls)
^^^^^^^^^^^^^^^^^^^^^^^^File"/home/user/.cache/pypoetry/virtualenvs/teste-4HLGbWUH-py3.12/lib/python3.12/site-packages/fastapi_utils/cbv.py", line57, 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", line112, in_register_endpointsraiseValueError("The provided routes should be of type APIRoute")
ValueError: Theprovidedroutesshouldbeof type APIRoute

To Reproduce

fromfastapiimportFastAPI, WebSocket, WebSocketDisconnect, APIRouterfromfastapi_utils.cbvimportcbvapp=FastAPI()
router=APIRouter()
@cbv(router)classWebSocketCBV:
@router.websocket("/ws")asyncdefwebsocket_endpoint(self, websocket: WebSocket):
awaitwebsocket.accept()
try:
whileTrue:
data=awaitwebsocket.receive_text()
awaitwebsocket.send_text(f"Message text was: {data}")
exceptWebSocketDisconnect:
print("Client disconnected")
app.include_router(router)
if__name__=="__main__":
importuvicornuvicorn.run(app, host="0.0.0.0", port=8000)

Expected behavior
It to work since in your code the class WebSocketRoute is supported

Environment:
Python Version: 3.12

Package Versions:

  • fastapi: 0.115.8
  • fastapi-utils: 0.8.0

Additional context
Suggested solution in PR #358

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions