fastapi
fastapi copied to clipboard
document response in depends
Is your feature request related to a problem? Please describe. No not related to a problem.
I'm using fastapi.security.api_key.APIKeyCookie as a depends and it may raise HTTPException(403, detail="Not authenticated") for any router it is used. So i have to document a 403 responses in all related router.
Describe the solution you'd like
Could there by a way when I'm using a class as Depends, document responses once as a class member or some thing else instead of document it many times in routers as fastAPI could find all depends of a router.
But... why don't you just add this response in router registration?
It should look like that:
# ...
app.include_router(my_router, responses={403: ...})
@prostomarkeloff Like why using depends instead of calling it in handlers...
@prostomarkeloff Like why using depends instead of calling it in handlers...
It doesn't look equal in count of written code😂
@prostomarkeloff Sorry, my apology.
Some of my routers from one APIRouter are not using same depdencies.
Yep, I want to have this in some way. But I have to figure out some other features and changes first. But I want to solve this in some way. 🤓
Sorry for the long delay! 🙈 I wanted to personally address each issue and they piled up through time, but now I'm checking each one in order.
@tiangolo , if you or anyone hasn't got a chance to look into this, shall I take a stab at this?
have been waiting for progress for 3 years…
I'm not sure I understand the issue, but won't a Custom Exception Handler do the trick?
from fastapi import Depends, HTTPException, APIRouter, status
class NotAuthenticatedException(HTTPException):
def __init__(self, detail="Not authenticated"):
super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail)
router = APIRouter()
@router.get("/protected-endpoint")
async def protected_endpoint(api_key: str = Depends(APIKeyCookie(secret="your_secret_key"))):
print("Hello world")
I'm not sure I understand the issue, but won't a Custom Exception Handler do the trick?
from fastapi import Depends, HTTPException, APIRouter, status class NotAuthenticatedException(HTTPException): def __init__(self, detail="Not authenticated"): super().__init__(status_code=status.HTTP_403_FORBIDDEN, detail=detail) router = APIRouter() @router.get("/protected-endpoint") async def protected_endpoint(api_key: str = Depends(APIKeyCookie(secret="your_secret_key"))): print("Hello world")
No, you didn't understand this issue. It's not about how to raise a exception or which exception to raise, but how to add a 403 response in openapi for all endpoint using a Depends, for example, APIKeyCookie
what we want:
extra 403 or 401 response from APIKeyCookie and make this:
@router.get("/protected-endpoint"})
async def protected_endpoint(api_key: str = Depends(APIKeyCookie(secret="your_secret_key"))):
print("Hello world")
to equal to this
@router.get("/protected-endpoint", responses={403: {...}})
async def protected_endpoint(api_key: str = Depends(APIKeyCookie(secret="your_secret_key"))):
print("Hello world")
Any progress in this issue? I have the same need to modify openapi from depends😅 Can i or someone else help you to make this possible?🙌