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 APIRouteTo 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
Describe the bug
When trying to add a @router.websocket endpoint to a CBV Router the following exception is throw:
To Reproduce
Expected behavior
It to work since in your code the class
WebSocketRouteis supportedEnvironment:
Python Version: 3.12
Package Versions:
Additional context
Suggested solution in PR #358