mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 22:04:48 +03:00
Merge 2d87d70d09
into 1b882f7281
This commit is contained in:
commit
a28689b3ca
|
@ -101,16 +101,26 @@ class SchemaGenerator(object):
|
||||||
if not links:
|
if not links:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# looks for overlapped actions for multiple http methods per action case
|
||||||
|
overlapped = {}
|
||||||
|
for category, action, link in links:
|
||||||
|
key = (category, action)
|
||||||
|
overlapped[key] = key in overlapped
|
||||||
|
|
||||||
# Generate the schema content structure, eg:
|
# Generate the schema content structure, eg:
|
||||||
# {'users': {'list': Link()}}
|
# {'users': {'list': Link()}}
|
||||||
content = {}
|
content = {}
|
||||||
for category, action, link in links:
|
for category, action, link in links:
|
||||||
|
# add suffix for overlapped actions
|
||||||
|
if overlapped[(category, action)]:
|
||||||
|
action = '%s_%s' % (action, link.action)
|
||||||
if category is None:
|
if category is None:
|
||||||
content[action] = link
|
content[action] = link
|
||||||
elif category in content:
|
|
||||||
content[category][action] = link
|
|
||||||
else:
|
else:
|
||||||
content[category] = {action: link}
|
if category not in content:
|
||||||
|
content[category] = {}
|
||||||
|
|
||||||
|
content[category][action] = link
|
||||||
|
|
||||||
# Return the schema document.
|
# Return the schema document.
|
||||||
return coreapi.Document(title=self.title, content=content, url=self.url)
|
return coreapi.Document(title=self.title, content=content, url=self.url)
|
||||||
|
|
|
@ -11,7 +11,7 @@ from rest_framework.routers import DefaultRouter
|
||||||
from rest_framework.schemas import SchemaGenerator
|
from rest_framework.schemas import SchemaGenerator
|
||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
||||||
|
|
||||||
|
|
||||||
class MockUser(object):
|
class MockUser(object):
|
||||||
|
@ -19,6 +19,11 @@ class MockUser(object):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class MockCoreapiObject(object):
|
||||||
|
def __eq__(self, value):
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class ExamplePagination(pagination.PageNumberPagination):
|
class ExamplePagination(pagination.PageNumberPagination):
|
||||||
page_size = 100
|
page_size = 100
|
||||||
|
|
||||||
|
@ -41,13 +46,13 @@ class ExampleViewSet(ModelViewSet):
|
||||||
filter_backends = [filters.OrderingFilter]
|
filter_backends = [filters.OrderingFilter]
|
||||||
serializer_class = ExampleSerializer
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
|
@detail_route(methods=['put', 'post'], serializer_class=AnotherSerializer)
|
||||||
def custom_action(self, request, pk):
|
def custom_action(self, request, pk):
|
||||||
return super(ExampleSerializer, self).retrieve(self, request)
|
pass
|
||||||
|
|
||||||
@list_route()
|
@list_route()
|
||||||
def custom_list_action(self, request):
|
def custom_list_action(self, request):
|
||||||
return super(ExampleViewSet, self).list(self, request)
|
pass
|
||||||
|
|
||||||
def get_serializer(self, *args, **kwargs):
|
def get_serializer(self, *args, **kwargs):
|
||||||
assert self.request
|
assert self.request
|
||||||
|
@ -55,6 +60,22 @@ class ExampleViewSet(ModelViewSet):
|
||||||
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleViewSet1(GenericViewSet):
|
||||||
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
|
@detail_route(methods=['post'])
|
||||||
|
def custom_action(self, request, pk):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ExampleViewSet2(GenericViewSet):
|
||||||
|
serializer_class = ExampleSerializer
|
||||||
|
|
||||||
|
@detail_route(methods=['post'])
|
||||||
|
def custom_action(self, request, pk):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ExampleView(APIView):
|
class ExampleView(APIView):
|
||||||
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
||||||
|
|
||||||
|
@ -70,10 +91,18 @@ router.register('example', ExampleViewSet, base_name='example')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^', include(router.urls))
|
url(r'^', include(router.urls))
|
||||||
]
|
]
|
||||||
|
|
||||||
urlpatterns2 = [
|
urlpatterns2 = [
|
||||||
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
||||||
]
|
]
|
||||||
|
|
||||||
|
router = DefaultRouter(schema_title='Example API' if coreapi else None)
|
||||||
|
router.register('example1', ExampleViewSet1, base_name='example')
|
||||||
|
router.register('example2', ExampleViewSet2, base_name='example')
|
||||||
|
urlpatterns3 = [
|
||||||
|
url(r'^', include(router.urls))
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||||
@override_settings(ROOT_URLCONF='tests.test_schemas')
|
@override_settings(ROOT_URLCONF='tests.test_schemas')
|
||||||
|
@ -119,9 +148,17 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
expected = coreapi.Document(
|
expected = coreapi.Document(
|
||||||
url='',
|
url='',
|
||||||
title='Example API',
|
title='Example API',
|
||||||
content={
|
content={'example': MockCoreapiObject()}
|
||||||
'example': {
|
)
|
||||||
'list': coreapi.Link(
|
self.assertEqual(response.data, expected)
|
||||||
|
|
||||||
|
def test_links(self):
|
||||||
|
client = APIClient()
|
||||||
|
client.force_authenticate(MockUser())
|
||||||
|
response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
expected_links = [
|
||||||
|
coreapi.Link( # list
|
||||||
url='/example/',
|
url='/example/',
|
||||||
action='get',
|
action='get',
|
||||||
fields=[
|
fields=[
|
||||||
|
@ -129,7 +166,7 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
coreapi.Field('ordering', required=False, location='query')
|
coreapi.Field('ordering', required=False, location='query')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'create': coreapi.Link(
|
coreapi.Link( # create
|
||||||
url='/example/',
|
url='/example/',
|
||||||
action='post',
|
action='post',
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
|
@ -138,14 +175,14 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
coreapi.Field('b', required=False, location='form')
|
coreapi.Field('b', required=False, location='form')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'retrieve': coreapi.Link(
|
coreapi.Link( # retrieve
|
||||||
url='/example/{pk}/',
|
url='/example/{pk}/',
|
||||||
action='get',
|
action='get',
|
||||||
fields=[
|
fields=[
|
||||||
coreapi.Field('pk', required=True, location='path')
|
coreapi.Field('pk', required=True, location='path')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'custom_action': coreapi.Link(
|
coreapi.Link( # custom_action post
|
||||||
url='/example/{pk}/custom_action/',
|
url='/example/{pk}/custom_action/',
|
||||||
action='post',
|
action='post',
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
|
@ -155,11 +192,21 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
coreapi.Field('d', required=False, location='form'),
|
coreapi.Field('d', required=False, location='form'),
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'custom_list_action': coreapi.Link(
|
coreapi.Link( # custom_action put
|
||||||
|
url='/example/{pk}/custom_action/',
|
||||||
|
action='put',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('c', required=True, location='form'),
|
||||||
|
coreapi.Field('d', required=False, location='form'),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
coreapi.Link( # custom_list_action
|
||||||
url='/example/custom_list_action/',
|
url='/example/custom_list_action/',
|
||||||
action='get'
|
action='get'
|
||||||
),
|
),
|
||||||
'update': coreapi.Link(
|
coreapi.Link( # update
|
||||||
url='/example/{pk}/',
|
url='/example/{pk}/',
|
||||||
action='put',
|
action='put',
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
|
@ -169,7 +216,7 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
coreapi.Field('b', required=False, location='form')
|
coreapi.Field('b', required=False, location='form')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'partial_update': coreapi.Link(
|
coreapi.Link( # partial_update
|
||||||
url='/example/{pk}/',
|
url='/example/{pk}/',
|
||||||
action='patch',
|
action='patch',
|
||||||
encoding='application/json',
|
encoding='application/json',
|
||||||
|
@ -179,21 +226,50 @@ class TestRouterGeneratedSchema(TestCase):
|
||||||
coreapi.Field('b', required=False, location='form')
|
coreapi.Field('b', required=False, location='form')
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
'destroy': coreapi.Link(
|
coreapi.Link( # destroy
|
||||||
url='/example/{pk}/',
|
url='/example/{pk}/',
|
||||||
action='delete',
|
action='delete',
|
||||||
fields=[
|
fields=[
|
||||||
coreapi.Field('pk', required=True, location='path')
|
coreapi.Field('pk', required=True, location='path')
|
||||||
]
|
]
|
||||||
)
|
),
|
||||||
}
|
]
|
||||||
}
|
|
||||||
)
|
response_links = response.data['example'].links.values()
|
||||||
self.assertEqual(response.data, expected)
|
for link in expected_links:
|
||||||
|
self.assertIn(link, response_links)
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
||||||
class TestSchemaGenerator(TestCase):
|
class TestSchemaGenerator(TestCase):
|
||||||
|
def test_similar_actions(self):
|
||||||
|
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns3)
|
||||||
|
schema = schema_generator.get_schema()
|
||||||
|
self.assertIn('example1', schema)
|
||||||
|
self.assertIn('example2', schema)
|
||||||
|
custom_action_1 = coreapi.Link(
|
||||||
|
url='/example1/{pk}/custom_action/',
|
||||||
|
action='post',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||||
|
coreapi.Field('b', required=False, location='form')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
custom_action_2 = coreapi.Link(
|
||||||
|
url='/example2/{pk}/custom_action/',
|
||||||
|
action='post',
|
||||||
|
encoding='application/json',
|
||||||
|
fields=[
|
||||||
|
coreapi.Field('pk', required=True, location='path'),
|
||||||
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
||||||
|
coreapi.Field('b', required=False, location='form')
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.assertIn(custom_action_1, schema['example1'].links.values())
|
||||||
|
self.assertIn(custom_action_2, schema['example2'].links.values())
|
||||||
|
|
||||||
def test_view(self):
|
def test_view(self):
|
||||||
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
||||||
schema = schema_generator.get_schema()
|
schema = schema_generator.get_schema()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user