Skip to content

Consider including list routes under associated categories when generating CoreAPI schema #4376

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

Closed
Closed
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
14 changes: 10 additions & 4 deletions rest_framework/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,21 @@ def get_key(self, path, method, callback):
Return a tuple of strings, indicating the identity to use for a
given endpoint. eg. ('users', 'list').
"""
actions = getattr(callback, 'actions', self.default_mapping)
action = actions[method.lower()]

list_route = None
if hasattr(callback.cls, action):
list_route_action = getattr(callback.cls, action)
if hasattr(list_route_action, 'bind_to_methods') and not getattr(list_route_action, 'detail', None):
list_route = list_route_action.kwargs.get('url_path', action)

category = None
for item in path.strip('/').split('/'):
if '{' in item:
if '{' in item or item == list_route:
break
category = item

actions = getattr(callback, 'actions', self.default_mapping)
action = actions[method.lower()]

if category:
return (category, action)
return (action,)
Expand Down
22 changes: 17 additions & 5 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from rest_framework import filters, pagination, permissions, serializers
from rest_framework.compat import coreapi
from rest_framework.decorators import detail_route
from rest_framework.decorators import detail_route, list_route
from rest_framework.response import Response
from rest_framework.routers import DefaultRouter
from rest_framework.schemas import SchemaGenerator
Expand Down Expand Up @@ -40,8 +40,12 @@ class ExampleViewSet(ModelViewSet):
serializer_class = ExampleSerializer

@detail_route(methods=['post'], serializer_class=AnotherSerializer)
def custom_action(self, request, pk):
return super(ExampleSerializer, self).retrieve(self, request)
def custom_detail_action(self, request, pk):
return super(ExampleViewSet, self).retrieve(self, request)

@list_route()
def custom_list_action(self, request):
return super(ExampleViewSet, self).list(self, request)


class ExampleView(APIView):
Expand Down Expand Up @@ -90,6 +94,10 @@ def test_anonymous_request(self):
fields=[
coreapi.Field('pk', required=True, location='path')
]
),
'custom_list_action': coreapi.Link(
url='/example/custom_list_action/',
action='get'
)
}
}
Expand Down Expand Up @@ -130,8 +138,8 @@ def test_authenticated_request(self):
coreapi.Field('pk', required=True, location='path')
]
),
'custom_action': coreapi.Link(
url='/example/{pk}/custom_action/',
'custom_detail_action': coreapi.Link(
url='/example/{pk}/custom_detail_action/',
action='post',
encoding='application/json',
fields=[
Expand All @@ -140,6 +148,10 @@ def test_authenticated_request(self):
coreapi.Field('d', required=False, location='form'),
]
),
'custom_list_action': coreapi.Link(
url='/example/custom_list_action/',
action='get'
),
'update': coreapi.Link(
url='/example/{pk}/',
action='put',
Expand Down
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