Skip to content

Fix detail_route and list_route overrides of serializer_class in schema generator #4331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 12 additions & 2 deletions rest_framework/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,22 @@ def get_serializer_fields(self, path, method, callback, view):
if method not in ('PUT', 'PATCH', 'POST'):
return []

if not hasattr(view, 'get_serializer_class'):
serializer_class = None

# looking for serializer_class override
if hasattr(callback, 'actions'):
func = getattr(view, callback.actions[method.lower()])
if 'serializer_class' in getattr(func, 'kwargs', ()):
serializer_class = func.kwargs['serializer_class']

if serializer_class is None and hasattr(view, 'get_serializer_class'):
serializer_class = view.get_serializer_class()

if serializer_class is None:
return []

fields = []

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

if isinstance(serializer, serializers.ListSerializer):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from rest_framework import filters, pagination, permissions, serializers
from rest_framework.compat import coreapi
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from rest_framework.routers import DefaultRouter
from rest_framework.schemas import SchemaGenerator
Expand All @@ -27,12 +28,21 @@ class ExampleSerializer(serializers.Serializer):
b = serializers.CharField(required=False)


class AnotherSerializer(serializers.Serializer):
c = serializers.CharField(required=True)
d = serializers.CharField(required=False)


class ExampleViewSet(ModelViewSet):
pagination_class = ExamplePagination
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
filter_backends = [filters.OrderingFilter]
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):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
Expand Down Expand Up @@ -120,6 +130,16 @@ def test_authenticated_request(self):
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(
url='/example/{pk}/',
action='put',
Expand Down