You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am adding the same route several times with different method to an app. This worked fine, but I needed to add name="request" to be able to identify which route is going to be used in a custom middleware.This ends up in an error about registering a resource more than once.
When inspecting the code around web_urldispatcher, I think that it should normally be accepted by the server:
# Reuse last added resource if path and name are the same
ifself._resources:
resource=self._resources[-1]
ifresource.name==nameandresource.raw_match(path):
returncast(Resource, resource)
But since the method DynamicResource#raw_match compares _formatter to the path, they are different: "/{path}" != "/{path:.*}". But I think we should probably match these correctly, no?
To Reproduce
run_routes= [
# Be aware that handling ANY_METHOD (*) conflicts with CORS preflight# since it needs to overtake the OPTIONS method. So we manually specify# all other methods (this is currently a bug in aiohttp-cors).## https://github.com/aio-libs/aiohttp-cors/issues/241web.route(method, "/{path:.*}", self.on_request, name="request")
formethodin (
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
)
]
app.add_routes(run_routes)
Expected behavior
it should reuse the resource instead of register a new one and failing
Logs/tracebacks
app.add_routes(run_routes)
File "/.../python3.11/site-packages/aiohttp/web_app.py", line 334, in add_routesreturnself.router.add_routes(routes)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1202, in add_routes
registered_routes.extend(route_def.register(self))
^^^^^^^^^^^^^^^^^^^^^^^^
File "/.../python3.11/site-packages/aiohttp/web_routedef.py", line 76, in registerreturn [reg(self.path, self.handler, **self.kwargs)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1168, in add_postreturnself.add_route(hdrs.METH_POST, path, handler, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1102, in add_route
resource =self.add_resource(path, name=name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1090, in add_resourceself.register_resource(resource)
File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1068, in register_resourceraiseValueError(
ValueError: Duplicate 'request', already handled by <DynamicResource 'request' /{path}>
Describe the bug
I am adding the same route several times with different method to an app. This worked fine, but I needed to add
name="request"
to be able to identify which route is going to be used in a custom middleware.This ends up in an error about registering a resource more than once.When inspecting the code around
web_urldispatcher
, I think that it should normally be accepted by the server:aiohttp/aiohttp/web_urldispatcher.py
Lines 1056 to 1060 in a4ed8c5
But since the method
DynamicResource#raw_match
compares_formatter
to the path, they are different:"/{path}" != "/{path:.*}"
. But I think we should probably match these correctly, no?To Reproduce
Expected behavior
it should reuse the resource instead of register a new one and failing
Logs/tracebacks
Python Version
aiohttp Version
multidict Version
yarl Version
OS
Related component
Server
Additional context
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: