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

'StaticResource' object has no attribute 'add_route' #151

Closed
kamikaze opened this issue Feb 1, 2018 · 5 comments
Closed

'StaticResource' object has no attribute 'add_route' #151

kamikaze opened this issue Feb 1, 2018 · 5 comments

Comments

@kamikaze
Copy link

kamikaze commented Feb 1, 2018

when router has a static resource in routes... then this code fails:

https://github.com/aio-libs/aiohttp-cors/blob/master/aiohttp_cors/urldispatcher_router_adapter.py#L341

with:
AttributeError: 'StaticResource' object has no attribute 'add_route'

@kamikaze
Copy link
Author

kamikaze commented Feb 1, 2018

actually trying to get aiohttp_swagger json file being exposed with CORS headers...

def setup_routes(app):
    app.router.add_routes(routes)
    setup_swagger(app, api_base_url='/api/v2', swagger_url='/api/v2/doc')
    cors = aiohttp_cors.setup(app, defaults={
        "*": aiohttp_cors.ResourceOptions(
            allow_credentials=True,
            expose_headers="*",
            allow_headers="*",
        )
    })

    for route in list(app.router.routes()):
        cors.add(route)

@asvetlov
Copy link
Member

asvetlov commented Feb 1, 2018

#130

@kamikaze
Copy link
Author

kamikaze commented Feb 1, 2018

in #130 author switched to cors.add(route) which is already in my example

@kamikaze
Copy link
Author

kamikaze commented Feb 1, 2018

Made this as a workaround. Isn't it a bug?

def setup_routes(app):
    app.router.add_routes(routes)
    setup_swagger(app, api_base_url='/api/v2', swagger_url='/api/v2/doc')

    cors = aiohttp_cors.setup(app, defaults={
        "*": aiohttp_cors.ResourceOptions(
            allow_credentials=True,
            expose_headers="*",
            allow_headers="*",
        )
    })

    for route in list(app.router.routes()):
        if not isinstance(route.resource, StaticResource): # <<< WORKAROUND
            cors.add(route)

@asvetlov
Copy link
Member

Discussed on gitter already but I copy the answer for future readers:
Replace iteration over routes with resources:

    for resource in list(app.router.resources()):
            cors.add(resource)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants