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.
-
First Check
Commit to Help
Example Code
Description
Running the code above and sending a get request to
/get
yields:It works fine
my_dependency
function runsTrying to connect to
/ws
endpoint on the other hand throws an error:From the server's output it's visible
my_var
can't be found becausemy_dependency
doesn't run.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 System
Linux
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
Python 3.10.4
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions