Skip to content
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
4 changes: 3 additions & 1 deletion rest_framework/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def get_api_endpoints(self, patterns, prefix=''):

for pattern in patterns:
path_regex = prefix + pattern.regex.pattern

if isinstance(pattern, RegexURLPattern):
path = self.get_path(path_regex)
callback = pattern.callback
Expand Down Expand Up @@ -253,6 +252,9 @@ def get_serializer_fields(self, path, method, callback, view):

fields = []

if not (hasattr(view, 'get_serializer_class') and callable(getattr(view, 'get_serializer_class'))):
return []

serializer_class = view.get_serializer_class()
serializer = serializer_class()

Expand Down
42 changes: 42 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

from rest_framework import filters, pagination, permissions, serializers
from rest_framework.compat import coreapi
from rest_framework.response import Response
from rest_framework.routers import DefaultRouter
from rest_framework.schemas import SchemaGenerator
from rest_framework.test import APIClient
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet


Expand All @@ -31,11 +34,24 @@ class ExampleViewSet(ModelViewSet):
serializer_class = ExampleSerializer


class ExampleView(APIView):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]

def get(self, request, *args, **kwargs):
return Response()

def post(self, request, *args, **kwargs):
return Response()


router = DefaultRouter(schema_title='Example API' if coreapi else None)
router.register('example', ExampleViewSet, base_name='example')
urlpatterns = [
url(r'^', include(router.urls))
]
urlpatterns2 = [
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
]


@unittest.skipUnless(coreapi, 'coreapi is not installed')
Expand Down Expand Up @@ -135,3 +151,29 @@ def test_authenticated_request(self):
}
)
self.assertEqual(response.data, expected)


@unittest.skipUnless(coreapi, 'coreapi is not installed')
class TestSchemaGenerator(TestCase):
def test_view(self):
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
schema = schema_generator.get_schema()
expected = coreapi.Document(
url='',
title='Test View',
content={
'example-view': {
'create': coreapi.Link(
url='/example-view/',
action='post',
fields=[]
),
'read': coreapi.Link(
url='/example-view/',
action='get',
fields=[]
)
}
}
)
self.assertEquals(schema, expected)