fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

document response in depends

Open trim21 opened this issue 6 years ago • 10 comments
trafficstars

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.

trim21 avatar Sep 04 '19 06:09 trim21

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 avatar Jan 03 '20 19:01 prostomarkeloff

@prostomarkeloff Like why using depends instead of calling it in handlers...

trim21 avatar Jan 03 '20 19:01 trim21

@prostomarkeloff Like why using depends instead of calling it in handlers...

It doesn't look equal in count of written code😂

prostomarkeloff avatar Jan 03 '20 19:01 prostomarkeloff

@prostomarkeloff Sorry, my apology.

Some of my routers from one APIRouter are not using same depdencies.

trim21 avatar Jan 03 '20 23:01 trim21

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 avatar Oct 22 '22 12:10 tiangolo

@tiangolo , if you or anyone hasn't got a chance to look into this, shall I take a stab at this?

iudeen avatar Aug 25 '23 20:08 iudeen

have been waiting for progress for 3 years…

trim21 avatar Aug 27 '23 18:08 trim21

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")

codespearhead avatar Apr 01 '24 18:04 codespearhead

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")

trim21 avatar Apr 01 '24 18:04 trim21

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?🙌

AlexeyReket avatar Aug 01 '24 17:08 AlexeyReket