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

fix custom http_method_names for actions #1184 #1185

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/']
Loading