-
First Check
Commit to Help
Example Codefrom fastapi import Depends, FastAPI,APIRouter,Request, WebSocket
def my_dependency(request: Request):
print("called_my_dependency")
request.state.my_var = "my_var"
app = FastAPI(
dependencies=[Depends(my_dependency)]
)
def get_test(request: Request):
print(request.state.my_var)
return request.state.my_var
async def ws_test(websocket: WebSocket):
print(websocket.state.my_var)
app.add_api_route('/get',get_test)
app.add_api_websocket_route('/ws',ws_test)DescriptionRunning the code above and sending a get request to It works fine Trying to connect to From the server's output it's visible I don't know if it's intended or a bug but, I would expect it to run on the websocket connection as well and be able to access the state variables on the request. I have also tried it with APIRouter and the results are the same. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.78.0 Python VersionPython 3.10.4 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 1 reply
-
|
I don't see how a dependency that takes an HTML Request as an arg would be expected to run on a websocket connection? Just because the Websocket and Request objects both have an attribute called βstateβ does not mean they are interchangeable. I think you need a different dependency that takes a Websocket as the argument type. |
Beta Was this translation helpful? Give feedback.
-
|
Both Did some digging on this behaviour. I don't know if it is a bug or intended, but at least I can explain why this is happening. It seems that global dependencies, that are set in the See routing.py -> APIRouter -> add_api_route(), line 532 onwards. However, when adding a Websocket route, this behaviour is not the same! When adding a websocket route to the You can circumvent this by adding the dependency directly in the path operation function; this is working code: from fastapi import Depends, FastAPI,APIRouter,Request, WebSocket
import uvicorn
def my_dependency(request: Request | WebSocket):
print("called_my_dependency")
request.state.my_var = "my_var"
app = FastAPI(
dependencies=[Depends(my_dependency)]
)
def get_test(request: Request):
print(request.state.my_var)
return request.state.my_var
async def ws_test(websocket: WebSocket, _ = Depends(my_dependency)):
print(websocket.state.my_var)
app.add_api_route('/get',get_test)
app.add_api_websocket_route('/ws',ws_test)
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000) |
Beta Was this translation helpful? Give feedback.
-
|
@JarroVGIT I tried to run your snippet and I got a Pydantic error that a Uniont of a Request and a WebSocket is not allowed: Is there potentially another way to use the same dependency for both a |
Beta Was this translation helpful? Give feedback.
-
|
Interesting that this doesnβt work anymore, somewhere in the pas year a breaking change was introduced. I can see it has something to do with the new mechanics on the response model (it actually tries to create a response field for some strange reason). I canβt think of any other way, sprry |
Beta Was this translation helpful? Give feedback.
-
|
You can make an abstract class with abstractmethod |
Beta Was this translation helpful? Give feedback.
-
|
In case anyone ends up here, this works [tested with fastapi 0.108.0]: |
Beta Was this translation helpful? Give feedback.
-
|
As described in documentation:
It works. |
Beta Was this translation helpful? Give feedback.
As described in documentation:
It works.