-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1059 from rommapp/misc/backend-handle-trailing-slash
misc: Make backend handle URLs with trailing slash
- Loading branch information
Showing
19 changed files
with
98 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Any, Callable | ||
|
||
from fastapi import APIRouter as FastAPIRouter | ||
from fastapi.types import DecoratedCallable | ||
|
||
|
||
class APIRouter(FastAPIRouter): | ||
"""FastAPI router that automatically adds an alternate route with a trailing slash. | ||
This is needed as FastAPI does not include a built-in way to handle routes with and without | ||
trailing slashes, without requiring a redirect or duplicating the route definition. | ||
Reference: https://github.com/fastapi/fastapi/discussions/7298 | ||
""" | ||
|
||
def api_route( | ||
self, path: str, *, include_in_schema: bool = True, **kwargs: Any | ||
) -> Callable[[DecoratedCallable], DecoratedCallable]: | ||
if path.endswith("/") and len(path) > 1: | ||
path = path[:-1] | ||
|
||
add_path = super().api_route( | ||
path, include_in_schema=include_in_schema, **kwargs | ||
) | ||
|
||
alternate_path = path + "/" | ||
add_alternate_path = super().api_route( | ||
alternate_path, include_in_schema=False, **kwargs | ||
) | ||
|
||
def decorator(func: DecoratedCallable) -> DecoratedCallable: | ||
add_alternate_path(func) | ||
return add_path(func) | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import itertools | ||
|
||
import pytest | ||
from fastapi import Request | ||
from utils.router import APIRouter | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"method, route_path", | ||
itertools.product( | ||
("get", "post", "put", "delete", "patch"), | ||
("/test", "/test/"), | ||
), | ||
) | ||
def test_route_path_with_trailing_slash(method, route_path): | ||
router = APIRouter() | ||
|
||
@router.get(route_path) | ||
@router.post(route_path) | ||
@router.put(route_path) | ||
@router.delete(route_path) | ||
@router.patch(route_path) | ||
def test_route(request: Request): | ||
return {"test": "test"} | ||
|
||
assert test_route(Request({"type": "http", "method": method, "url": "/test"})) == { | ||
"test": "test" | ||
} | ||
assert test_route(Request({"type": "http", "method": method, "url": "/test/"})) == { | ||
"test": "test" | ||
} |