Skip to content

Schema: Exclude OPTIONS/HEAD for ViewSet actions #5532

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

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
9 changes: 4 additions & 5 deletions rest_framework/schemas/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,11 @@ def get_allowed_methods(self, callback):
if hasattr(callback, 'actions'):
actions = set(callback.actions.keys())
http_method_names = set(callback.cls.http_method_names)
return [method.upper() for method in actions & http_method_names]
methods = [method.upper() for method in actions & http_method_names]
else:
methods = callback.cls().allowed_methods

return [
method for method in
callback.cls().allowed_methods if method not in ('OPTIONS', 'HEAD')
]
return [method for method in methods if method not in ('OPTIONS', 'HEAD')]


class SchemaGenerator(object):
Expand Down
50 changes: 50 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,3 +901,53 @@ class TestView(generics.RetrieveAPIView):

is_list = is_list_view(path, method, view)
assert not is_list, "RetrieveAPIView subclasses should not be classified as list views."


def test_head_and_options_methods_are_excluded():
"""
Regression test for #5528
https://github.com/encode/django-rest-framework/issues/5528

Viewset OPTIONS actions were not being correctly excluded

Initial cases here shown to be working as expected.
"""

@api_view(['options', 'get'])
def fbv(request):
pass

inspector = EndpointEnumerator()

path = '/a/path/'
callback = fbv

assert inspector.should_include_endpoint(path, callback)
assert inspector.get_allowed_methods(callback) == ["GET"]

class AnAPIView(APIView):

def get(self, request, *args, **kwargs):
pass

def options(self, request, *args, **kwargs):
pass

callback = AnAPIView.as_view()

assert inspector.should_include_endpoint(path, callback)
assert inspector.get_allowed_methods(callback) == ["GET"]

class AViewSet(ModelViewSet):

@detail_route(methods=['options', 'get'])
def custom_action(self, request, pk):
pass

callback = AViewSet.as_view({
"options": "custom_action",
"get": "custom_action"
})

assert inspector.should_include_endpoint(path, callback)
assert inspector.get_allowed_methods(callback) == ["GET"]
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy