fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

Add redirect_slashes parameter to FastAPI and pass it to router

Open cyberlis opened this issue 4 years ago β€’ 14 comments
trafficstars

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.

cyberlis avatar Jun 29 '21 19:06 cyberlis

please review @tiangolo

cyberlis avatar Aug 02 '21 10:08 cyberlis

Hey @tiangolo, any news on this pr? It solves a lot of hassle with 'slash/no slash' related redirects @Kludex @jaystone776

StashOfCode avatar Sep 08 '21 18:09 StashOfCode

@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.

sk- avatar Oct 06 '22 21:10 sk-

πŸ“ Docs preview for commit 434f67c31b9d3b0c6aa596ba651c867dd39c68ce at: https://63457ff2dc12071d5f3bbfa3--fastapi.netlify.app

github-actions[bot] avatar Oct 11 '22 14:10 github-actions[bot]

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%> (ΓΈ)

... and 2 files with indirect coverage changes

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov[bot] avatar Oct 14 '22 11:10 codecov[bot]

πŸ“ Docs preview for commit 316ad61f6b8a27ac622f5b8ba7bd4995ae1ef462 at: https://63494c3b2c995530f46aa4ab--fastapi.netlify.app

github-actions[bot] avatar Oct 14 '22 11:10 github-actions[bot]

@Kludex @tiangolo please review PR

cyberlis avatar Oct 14 '22 12:10 cyberlis

πŸ“ Docs preview for commit 4bfaa009c075e3c73ab701d5cc4ee6513cb9720c at: https://6351aa4b4b09510865226f15--fastapi.netlify.app

github-actions[bot] avatar Oct 20 '22 20:10 github-actions[bot]

πŸ“ Docs preview for commit 56c7bd4921aa102067d6a071d79880b67e2bd538 at: https://639ce3792b35ad02dfe481d1--fastapi.netlify.app

github-actions[bot] avatar Dec 16 '22 21:12 github-actions[bot]

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!

xpinguin avatar Apr 27 '23 09:04 xpinguin

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

cyberlis avatar Apr 28 '23 21:04 cyberlis

πŸ“ Docs preview for commit 22499af015d2cac75165a7a451ae824dc0944f64 at: https://644c43c0587f9e1ec747a7b2--fastapi.netlify.app

github-actions[bot] avatar Apr 28 '23 22:04 github-actions[bot]

Hey @tiangolo, @Kludex This PR is ready to be merged It has tests and people still need it

Could you review it, please

cyberlis avatar Apr 28 '23 22:04 cyberlis

Starlette class doesn't have this parameter. It only exists on the Router class in Starlette. jfyk @tiangolo

Kludex avatar Apr 29 '23 13:04 Kludex

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. πŸŽ‰

tiangolo avatar Jun 22 '23 10:06 tiangolo