encode / starlette

The little ASGI framework that shines. 🌟
https://www.starlette.io/
BSD 3-Clause "New" or "Revised" License
10.31k stars 949 forks source link

Ensure accurate `root_path` removal in `get_route_path` function #2600

Closed gabriel-f-santos closed 3 months ago

gabriel-f-santos commented 6 months ago

Issue: https://github.com/encode/starlette/issues/2599 Discussion: https://github.com/encode/starlette/discussions/2495

Summary

Change regex used to remove root_path to consider only the root path without match simillar endpoint that contains the root_path as substring. ex:

root_path = "product"
scope["path"] = "products/all"

before:

re.sub(r"^" + root_path, "", scope["path"]) -> 's/all'

after:

re.sub(r"^" + root_path + r"(?=/|$)", "", scope["path"]) -> '/products/all'

Checklist

Kludex commented 3 months ago

Is there a way we can use removeprefix instead?