fastapi
fastapi copied to clipboard
Add redirect_slashes parameter to FastAPI and pass it to router
Right now redirect_slashes parameter in sub routers created with APIRouter ignored, because only main router's redirect_slashes inside FastAPI class is taken into account. FastAPI class creates new router without redirect_slashes argument and that's why router always uses default value redirect_slashes=True and always redirects on trailing slashes.
https://github.com/tiangolo/fastapi/blob/master/fastapi/applications.py#L69
Here is how to reproduce this problem
from fastapi import FastAPI
from fastapi.routing import APIRouter
router = APIRouter(redirect_slashes=False)
@router.get('/hello/')
def hello_page() -> str:
return 'Hello, World!'
app = FastAPI()
app.include_router(router)
And if you request page http://127.0.0.1:8080/hello (without trailing slash) you will be redirected even with redirect_slashes=False
uvicorn main:app
INFO: Started server process [96046]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:57330 - "GET /hello HTTP/1.1" 307 Temporary Redirect
INFO: 127.0.0.1:57330 - "GET /hello/ HTTP/1.1" 200 OK
I added redirect_slashes to __init__ method of FastAPI class and passed it to router's constructor.
That way I can change trailing slashes redirect behaviour globally for application instance.
please review @tiangolo
Hey @tiangolo, any news on this pr? It solves a lot of hassle with 'slash/no slash' related redirects @Kludex @jaystone776
@Kludex @tiangolo is there any chance this PR could be reviewed? I did review the code myself and it looks good and tested.
Some others have also spent the time writing a PR solving the same issue. See https://github.com/tiangolo/fastapi/pull/5392.
Our motivation for having this merged: We have stumbled across this issue, because some times customers make the mistake of adding/removing the trailing slash which increases the latency and later on they complain that our provided endpoints are slow.
It'd be great if we could control whether redirect_slashes is set.
π Docs preview for commit 434f67c31b9d3b0c6aa596ba651c867dd39c68ce at: https://63457ff2dc12071d5f3bbfa3--fastapi.netlify.app
Codecov Report
Patch coverage: 100.00% and no project coverage change.
Comparison is base (
cf73051) 100.00% compared to head (4bfaa00) 100.00%.
:exclamation: Current head 4bfaa00 differs from pull request most recent head 3ecac8b. Consider uploading reports for the commit 3ecac8b to get more accurate results
Additional details and impacted files
@@ Coverage Diff @@
## master #3432 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 540 541 +1
Lines 13969 13972 +3
=========================================
+ Hits 13969 13972 +3
| Impacted Files | Coverage Ξ | |
|---|---|---|
| fastapi/applications.py | 100.00% <ΓΈ> (ΓΈ) |
|
| tests/test_router_redirect_slashes.py | 100.00% <100.00%> (ΓΈ) |
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.
π Docs preview for commit 316ad61f6b8a27ac622f5b8ba7bd4995ae1ef462 at: https://63494c3b2c995530f46aa4ab--fastapi.netlify.app
@Kludex @tiangolo please review PR
π Docs preview for commit 4bfaa009c075e3c73ab701d5cc4ee6513cb9720c at: https://6351aa4b4b09510865226f15--fastapi.netlify.app
π Docs preview for commit 56c7bd4921aa102067d6a071d79880b67e2bd538 at: https://639ce3792b35ad02dfe481d1--fastapi.netlify.app
Hi guys, could you please update us about the status of this PR? We're waiting for this feature in master for a long time already and custom hacks aren't good. Thanks a lot!
Hi guys, could you please update us about the status of this PR? We're waiting for this feature in master for a long time already and custom hacks aren't good. Thanks a lot!
I'll update it tomorrow
π Docs preview for commit 22499af015d2cac75165a7a451ae824dc0944f64 at: https://644c43c0587f9e1ec747a7b2--fastapi.netlify.app
Hey @tiangolo, @Kludex This PR is ready to be merged It has tests and people still need it
Could you review it, please
Starlette class doesn't have this parameter. It only exists on the Router class in Starlette. jfyk @tiangolo
Makes sense, thank you @cyberlis! π
Thanks everyone for the reviews, especially for commenting that you tried out the code and confirming that this works and is useful for you. π π°
This will be available in FastAPI 0.98.0, released in the next hours. π