Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource reuse not working for dynamic resources #7624

Closed
1 task done
chamini2 opened this issue Sep 20, 2023 · 0 comments · Fixed by #8998
Closed
1 task done

Resource reuse not working for dynamic resources #7624

chamini2 opened this issue Sep 20, 2023 · 0 comments · Fixed by #8998
Labels

Comments

@chamini2
Copy link

chamini2 commented Sep 20, 2023

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:

# Reuse last added resource if path and name are the same
if self._resources:
resource = self._resources[-1]
if resource.name == name and resource.raw_match(path):
return cast(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/241
    web.route(method, "/{path:.*}", self.on_request, name="request")
    for method in (
        "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_routes
    return self.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 register
    return [reg(self.path, self.handler, **self.kwargs)]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1168, in add_post
    return self.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_resource
    self.register_resource(resource)
  File "/.../python3.11/site-packages/aiohttp/web_urldispatcher.py", line 1068, in register_resource
    raise ValueError(
ValueError: Duplicate 'request', already handled by <DynamicResource 'request'  /{path}>

Python Version

python --version
Python 3.11.0

aiohttp Version

Name: aiohttp
Version: 3.8.5
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author:
Author-email:
License: Apache 2
Location: /.../python3.11/site-packages
Requires: aiosignal, async-timeout, attrs, charset-normalizer, frozenlist, multidict, yarl
Required-by: aiohttp-cors, auth0-python, cloud-sql-python-connector, isolate_controller, kubernetes-asyncio, pyjwt-key-fetcher

multidict Version

python -m pip show multidict
Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /.../python3.11/site-packages
Requires:
Required-by: aiohttp, yarl

yarl Version

python -m pip show yarl

Name: yarl
Version: 1.9.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /.../python3.11/site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

❯ uname -a
Darwin MattBookAir.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:43 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T8112 arm64

Related component

Server

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant