From f5bf86ac8429c4731f5d0783ea495e259c1eb19c Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 4 Sep 2024 14:03:49 +0100 Subject: [PATCH] [PR #8998/875f23d5 backport][3.10] Fix resource reuse with regex paths (#9016) **This is a backport of PR #8998 as merged into master (875f23d516220a1cf849eb4a6d758edb8bff1f55).** Co-authored-by: Sam Bull --- CHANGES/8998.bugfix.rst | 1 + aiohttp/web_urldispatcher.py | 3 ++- tests/test_web_urldispatcher.py | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 CHANGES/8998.bugfix.rst diff --git a/CHANGES/8998.bugfix.rst b/CHANGES/8998.bugfix.rst new file mode 100644 index 00000000000..1b6b189e7ea --- /dev/null +++ b/CHANGES/8998.bugfix.rst @@ -0,0 +1 @@ +Fixed an error when trying to add a route for multiple methods with a path containing a regex pattern -- by :user:`Dreamsorcerer`. diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index a1df64b8e61..0b300e84da1 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -456,6 +456,7 @@ class DynamicResource(Resource): def __init__(self, path: str, *, name: Optional[str] = None) -> None: super().__init__(name=name) + self._orig_path = path pattern = "" formatter = "" for part in ROUTE_RE.split(path): @@ -508,7 +509,7 @@ def _match(self, path: str) -> Optional[Dict[str, str]]: } def raw_match(self, path: str) -> bool: - return self._formatter == path + return self._orig_path == path def get_info(self) -> _InfoDict: return {"formatter": self._formatter, "pattern": self._pattern} diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py index 8a97acf504d..7991cfe821e 100644 --- a/tests/test_web_urldispatcher.py +++ b/tests/test_web_urldispatcher.py @@ -703,10 +703,11 @@ async def handler(request: web.Request) -> web.Response: @pytest.mark.parametrize( "path", - [ + ( "/a", "/{a}", - ], + "/{a:.*}", + ), ) def test_reuse_last_added_resource(path: str) -> None: # Test that adding a route with the same name and path of the last added