Include kwargs passed to 'as_view' when generating schemas (#4359)

This commit is contained in:
Tom Christie 2016-08-05 11:19:39 +01:00 committed by GitHub
parent 11a2468379
commit d5178c9246
3 changed files with 23 additions and 0 deletions

View File

@ -195,6 +195,8 @@ class SchemaGenerator(object):
Return a `coreapi.Link` instance for the given endpoint. Return a `coreapi.Link` instance for the given endpoint.
""" """
view = callback.cls() view = callback.cls()
for attr, val in getattr(callback, 'initkwargs', {}).items():
setattr(view, attr, val)
fields = self.get_path_fields(path, method, callback, view) fields = self.get_path_fields(path, method, callback, view)
fields += self.get_serializer_fields(path, method, callback, view) fields += self.get_serializer_fields(path, method, callback, view)

View File

@ -97,6 +97,7 @@ class ViewSetMixin(object):
# generation can pick out these bits of information from a # generation can pick out these bits of information from a
# resolved URL. # resolved URL.
view.cls = cls view.cls = cls
view.initkwargs = initkwargs
view.suffix = initkwargs.get('suffix', None) view.suffix = initkwargs.get('suffix', None)
view.actions = actions view.actions = actions
return csrf_exempt(view) return csrf_exempt(view)

View File

@ -5,6 +5,7 @@ from django.test import TestCase, override_settings
from rest_framework import filters, pagination, permissions, serializers from rest_framework import filters, pagination, permissions, serializers
from rest_framework.compat import coreapi from rest_framework.compat import coreapi
from rest_framework.decorators import detail_route
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from rest_framework.schemas import SchemaGenerator from rest_framework.schemas import SchemaGenerator
@ -27,12 +28,21 @@ class ExampleSerializer(serializers.Serializer):
b = serializers.CharField(required=False) b = serializers.CharField(required=False)
class AnotherSerializer(serializers.Serializer):
c = serializers.CharField(required=True)
d = serializers.CharField(required=False)
class ExampleViewSet(ModelViewSet): class ExampleViewSet(ModelViewSet):
pagination_class = ExamplePagination pagination_class = ExamplePagination
permission_classes = [permissions.IsAuthenticatedOrReadOnly] permission_classes = [permissions.IsAuthenticatedOrReadOnly]
filter_backends = [filters.OrderingFilter] filter_backends = [filters.OrderingFilter]
serializer_class = ExampleSerializer serializer_class = ExampleSerializer
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
def custom_action(self, request, pk):
return super(ExampleSerializer, self).retrieve(self, request)
class ExampleView(APIView): class ExampleView(APIView):
permission_classes = [permissions.IsAuthenticatedOrReadOnly] permission_classes = [permissions.IsAuthenticatedOrReadOnly]
@ -120,6 +130,16 @@ class TestRouterGeneratedSchema(TestCase):
coreapi.Field('pk', required=True, location='path') coreapi.Field('pk', required=True, location='path')
] ]
), ),
'custom_action': coreapi.Link(
url='/example/{pk}/custom_action/',
action='post',
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'),
]
),
'update': coreapi.Link( 'update': coreapi.Link(
url='/example/{pk}/', url='/example/{pk}/',
action='put', action='put',