From 6b857ef9cf53cea601ae5254de9c092058aa5b70 Mon Sep 17 00:00:00 2001 From: Tommy Beadle Date: Thu, 12 Sep 2024 12:11:09 -0400 Subject: [PATCH] Support async view methods in extend_schema_view. --- drf_spectacular/drainage.py | 8 ++++++-- drf_spectacular/openapi.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/drf_spectacular/drainage.py b/drf_spectacular/drainage.py index 9b47dc3e..b25f2626 100644 --- a/drf_spectacular/drainage.py +++ b/drf_spectacular/drainage.py @@ -182,8 +182,12 @@ def get_view_method_names(view, schema=None) -> List[str]: return [ item for item in dir(view) if callable(getattr(view, item)) and ( item in view.http_method_names - or item in schema.method_mapping.values() - or item == 'list' + or ( + item in schema.async_method_mapping.values() + if view.view_is_async + else item in schema.method_mapping.values() + ) + or item == ('alist' if view.view_is_async else 'list') or hasattr(getattr(view, item), 'mapping') ) ] diff --git a/drf_spectacular/openapi.py b/drf_spectacular/openapi.py index 8258b532..78a6b0fa 100644 --- a/drf_spectacular/openapi.py +++ b/drf_spectacular/openapi.py @@ -57,6 +57,13 @@ class AutoSchema(ViewInspector): 'patch': 'partial_update', 'delete': 'destroy', } + async_method_mapping = { + 'get': 'aretrieve', + 'post': 'acreate', + 'put': 'aupdate', + 'patch': 'partial_aupdate', + 'delete': 'adestroy', + } def get_operation( self,