From 7ea0710abb6ed51fcf245276188f578ce9ef60c2 Mon Sep 17 00:00:00 2001 From: "T. Franzel" Date: Mon, 26 Feb 2024 10:55:57 +0100 Subject: [PATCH] fix custom http_method_names for actions #1184 --- drf_spectacular/generators.py | 6 +++++- tests/test_regressions.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/drf_spectacular/generators.py b/drf_spectacular/generators.py index ac53f253..15f7ba9a 100644 --- a/drf_spectacular/generators.py +++ b/drf_spectacular/generators.py @@ -79,7 +79,11 @@ def _get_api_endpoints(self, patterns, prefix): def get_allowed_methods(self, callback): if hasattr(callback, 'actions'): actions = set(callback.actions) - http_method_names = set(callback.cls.http_method_names) + if 'http_method_names' in callback.initkwargs: + http_method_names = set(callback.initkwargs['http_method_names']) + else: + http_method_names = set(callback.cls.http_method_names) + methods = [method.upper() for method in actions & http_method_names] else: # pass to constructor allowed method names to get valid ones diff --git a/tests/test_regressions.py b/tests/test_regressions.py index c2b2e396..81879fa3 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -3328,3 +3328,23 @@ def view_func(request, format=None): assert get_response_schema(schema['paths']['/x']['post']) == ref_schema assert get_request_schema(schema['paths']['/x']['post']) == ref_schema + + +def test_customized_http_method_names(no_warnings): + class XViewSet(viewsets.ModelViewSet): + http_method_names = ['get', 'options', 'head'] + + serializer_class = SimpleSerializer + queryset = SimpleModel.objects.none() + + @action( + detail=True, + methods=['post'], + http_method_names=['post'], + ) + def favorite(self, request, pk=None): + pass # pragma: no cover + + schema = generate_schema('m', XViewSet) + + assert list(schema['paths'].keys()) == ['/m/', '/m/{id}/', '/m/{id}/favorite/']