Skip to content

Commit d5178c9

Browse files
authored
Include kwargs passed to 'as_view' when generating schemas (#4359)
1 parent 11a2468 commit d5178c9

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

rest_framework/schemas.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def get_link(self, path, method, callback):
195195
Return a `coreapi.Link` instance for the given endpoint.
196196
"""
197197
view = callback.cls()
198+
for attr, val in getattr(callback, 'initkwargs', {}).items():
199+
setattr(view, attr, val)
198200

199201
fields = self.get_path_fields(path, method, callback, view)
200202
fields += self.get_serializer_fields(path, method, callback, view)

rest_framework/viewsets.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def view(request, *args, **kwargs):
9797
# generation can pick out these bits of information from a
9898
# resolved URL.
9999
view.cls = cls
100+
view.initkwargs = initkwargs
100101
view.suffix = initkwargs.get('suffix', None)
101102
view.actions = actions
102103
return csrf_exempt(view)

tests/test_schemas.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from rest_framework import filters, pagination, permissions, serializers
77
from rest_framework.compat import coreapi
8+
from rest_framework.decorators import detail_route
89
from rest_framework.response import Response
910
from rest_framework.routers import DefaultRouter
1011
from rest_framework.schemas import SchemaGenerator
@@ -27,12 +28,21 @@ class ExampleSerializer(serializers.Serializer):
2728
b = serializers.CharField(required=False)
2829

2930

31+
class AnotherSerializer(serializers.Serializer):
32+
c = serializers.CharField(required=True)
33+
d = serializers.CharField(required=False)
34+
35+
3036
class ExampleViewSet(ModelViewSet):
3137
pagination_class = ExamplePagination
3238
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
3339
filter_backends = [filters.OrderingFilter]
3440
serializer_class = ExampleSerializer
3541

42+
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
43+
def custom_action(self, request, pk):
44+
return super(ExampleSerializer, self).retrieve(self, request)
45+
3646

3747
class ExampleView(APIView):
3848
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
@@ -120,6 +130,16 @@ def test_authenticated_request(self):
120130
coreapi.Field('pk', required=True, location='path')
121131
]
122132
),
133+
'custom_action': coreapi.Link(
134+
url='/example/{pk}/custom_action/',
135+
action='post',
136+
encoding='application/json',
137+
fields=[
138+
coreapi.Field('pk', required=True, location='path'),
139+
coreapi.Field('c', required=True, location='form'),
140+
coreapi.Field('d', required=False, location='form'),
141+
]
142+
),
123143
'update': coreapi.Link(
124144
url='/example/{pk}/',
125145
action='put',

0 commit comments

Comments
 (0)
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