Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint with limiter does not show 429 as a possible answer on the documentation page #74

Open
AntonGsv opened this issue Dec 8, 2021 · 3 comments

Comments

@AntonGsv
Copy link

AntonGsv commented Dec 8, 2021

Endpoint with limiter does not show 429 as a possible answer on the documentation page

@twcurrie
Copy link
Collaborator

twcurrie commented Dec 9, 2021

To elaborate, the generated OpenAPI docs available for the service at /docs (or /redocs?) do not show that 429 is a possible response for an endpoint with the rate limiter configured?

@AntonGsv
Copy link
Author

AntonGsv commented Dec 9, 2021

@twcurrie Yes, right now the decorator has no effect on OpenAPI generation.

from fastapi import FastAPI,  Request, Response
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)


@app.get("/mars")
@limiter.limit("5/minute")
async def homepage(request: Request, response: Response):
    return {"key": "value"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="127.0.0.1", reload=False, port=int("8000"))

I have to specify the error manually to get the desired result:

from fastapi import FastAPI, Request, Response
from pydantic import BaseModel
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded

limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)


class HTTPException429(BaseModel):
    error: str = "Rate limit exceeded: X per Y minute"


@app.get("/mars", responses={429: {"model": HTTPException429}})
@limiter.limit("5/minute")
async def homepage(request: Request, response: Response):
    return {"key": "value"}


if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="127.0.0.1", reload=False, port=int("8000"))

Since the decorator before app.get does not work is there an alternative way to automatically generate 429 response code for OpenAPI?

@twcurrie
Copy link
Collaborator

twcurrie commented Dec 9, 2021

I'd need to review the internals of FastAPI's docs generation to identify a way to hook into that process, but I agree, that should be surfaced within the OpenAPI documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants