Skip to content

Fix 'Method Not Allowed' on class based views - #178

Open
levkovskyi wants to merge 2 commits into
aio-libs:masterfrom
levkovskyi:master
Open

levkovskyi wants to merge 2 commits into
aio-libs:masterfrom
levkovskyi:master

Conversation

@levkovskyi

Copy link
Copy Markdown

Fix for #177

@alexmick

Copy link
Copy Markdown

Sorry for digging this up, any reason to not merge these changes ? Have been coming across this issue a lot lately !

@attie

attie commented Feb 14, 2020 •

Copy link
Copy Markdown

Please could we get this merged? I've also run into this issue, and the patch resovles it for me.

This is my use-case:

router = web.RouteTableDef()

@router.view('/my_endpoint')
class MyEndpoint(web.View, CorsViewMixin):

    async def post(self):
        print(self.request)
        raise web.HTTPOk()

When performing a GET against this, the client sees a 500, and the console sees the KeyError.

After applying the patch, the issue is resolved, and the client sees the correct 405.

@tommmlij

Copy link
Copy Markdown

Is there a plan to merge this?

This is completely breaking using views with the library...

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Cykooz

Cykooz commented Nov 27, 2020

Copy link
Copy Markdown
Member

Please, merge this PR. I think that a signing of Contributor License Agreement is not working.
изображение

@tzoiker

tzoiker commented Aug 29, 2022

Copy link
Copy Markdown

Any chance for merging it?

@attie-argentum

Copy link
Copy Markdown

Somehow, my last comment was 6 years ago, and I've just run into this again - please could we have some feedback / get it merged? 😀

cc: @rustky @asvetlov @Dreamsorcerer

@attie-argentum

Copy link
Copy Markdown

Monkey patch, if anyone else finds it useful:

def __patch_aiohttp_cors():                                                                         
    """                                                                                             
    aiohttp_cors.mixin.CorsViewMixin.get_request_config will raise a KeyError if                    
    the requested method isn't available...                                                         
                                                                                                    
    This function will monkey-patch it to return 405 Method Not Allowed instead.                    
                                                                                                    
    see: https://github.com/aio-libs/aiohttp-cors/pull/178                                          
    """                                                                                             
    from aiohttp import web                                                                         
    from aiohttp_cors.urldispatcher_router_adapter import (                                         
        ResourcesUrlDispatcherRouterAdapter,                                                        
        _is_web_view,                                                                               
    )                                                                                               
                                                                                                    
    if getattr(ResourcesUrlDispatcherRouterAdapter, '_patch_vuKPo6w5', False):                      
        return                                                                                      
                                                                                                    
    def vuKPo6w5__request_resource(self, request: web.Request) -> web.Resource:                     
        route = self._request_route(request)                                                        
        if _is_web_view(route, strict=False) and not hasattr(route.handler, request.method.lower()):
            resource = None                                                                         
        else:                                                                                       
            resource = route.resource                                                               
        return resource                                                                             
                                                                                                    
    ResourcesUrlDispatcherRouterAdapter._request_resource = vuKPo6w5__request_resource              
    ResourcesUrlDispatcherRouterAdapter._patch_vuKPo6w5 = True                                      
                                                                                                    
__patch_aiohttp_cors()                                                                              

@Dreamsorcerer

Copy link
Copy Markdown
Member

Was looking at it this week. Going to review in the near future.

@attie-argentum

Copy link
Copy Markdown

Amazing, thanks!

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

10 participants