Skip to content

Commit

Permalink
Merge pull request #1185 from tfranzel/custom_method_names
Browse files Browse the repository at this point in the history
fix custom http_method_names for actions #1184
  • Loading branch information
tfranzel authored Feb 26, 2024
2 parents b65ae61 + 7ea0710 commit 6f09242
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion drf_spectacular/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/']

0 comments on commit 6f09242

Please sign in to comment.