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