Have is_list_view recognise RetrieveModel… views (#5480)

Fixes #5165
This commit is contained in:
Carlton Gibson 2017-10-05 20:41:14 +02:00 committed by GitHub
parent d8da6bb29b
commit 2edeb74e0e
2 changed files with 16 additions and 0 deletions

View File

@ -3,6 +3,7 @@ utils.py # Shared helper functions
See schemas.__init__.py for package overview. See schemas.__init__.py for package overview.
""" """
from rest_framework.mixins import RetrieveModelMixin
def is_list_view(path, method, view): def is_list_view(path, method, view):
@ -15,6 +16,8 @@ def is_list_view(path, method, view):
if method.lower() != 'get': if method.lower() != 'get':
return False return False
if isinstance(view, RetrieveModelMixin):
return False
path_components = path.strip('/').split('/') path_components = path.strip('/').split('/')
if path_components and '{' in path_components[-1]: if path_components and '{' in path_components[-1]:
return False return False

View File

@ -19,6 +19,7 @@ from rest_framework.schemas import (
AutoSchema, ManualSchema, SchemaGenerator, get_schema_view AutoSchema, ManualSchema, SchemaGenerator, get_schema_view
) )
from rest_framework.schemas.generators import EndpointEnumerator from rest_framework.schemas.generators import EndpointEnumerator
from rest_framework.schemas.utils import is_list_view
from rest_framework.test import APIClient, APIRequestFactory from rest_framework.test import APIClient, APIRequestFactory
from rest_framework.utils import formatting from rest_framework.utils import formatting
from rest_framework.views import APIView from rest_framework.views import APIView
@ -808,3 +809,15 @@ class TestURLNamingCollisions(TestCase):
with pytest.raises(ValueError): with pytest.raises(ValueError):
generator.get_schema() generator.get_schema()
def test_is_list_view_recognises_retrieve_view_subclasses():
class TestView(generics.RetrieveAPIView):
pass
path = '/looks/like/a/list/view/'
method = 'get'
view = TestView()
is_list = is_list_view(path, method, view)
assert not is_list, "RetrieveAPIView subclasses should not be classified as list views."