From 32a0b62508fc6a83bc7fd80d362d67ca35b603f9 Mon Sep 17 00:00:00 2001 From: Lim H Date: Sat, 12 Aug 2017 18:59:03 +0100 Subject: [PATCH 1/2] Fix introspection of list field in schema --- rest_framework/schemas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 875f9454b..437413355 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -30,7 +30,7 @@ def field_to_schema(field): title = force_text(field.label) if field.label else '' description = force_text(field.help_text) if field.help_text else '' - if isinstance(field, serializers.ListSerializer): + if isinstance(field, (serializers.ListSerializer, serializers.ListField)): child_schema = field_to_schema(field.child) return coreschema.Array( items=child_schema, From 56021f9e7769138e0ae69115f01ed45b5bc8be3f Mon Sep 17 00:00:00 2001 From: Lim H Date: Sun, 20 Aug 2017 17:12:56 +0100 Subject: [PATCH 2/2] Add tests for list field to schema --- tests/test_schemas.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 2b8673480..b435dfdd7 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -39,6 +39,11 @@ class ExampleSerializer(serializers.Serializer): hidden = serializers.HiddenField(default='hello') +class AnotherSerializerWithListFields(serializers.Serializer): + a = serializers.ListField(child=serializers.IntegerField()) + b = serializers.ListSerializer(child=serializers.CharField()) + + class AnotherSerializer(serializers.Serializer): c = serializers.CharField(required=True) d = serializers.CharField(required=False) @@ -57,6 +62,13 @@ class ExampleViewSet(ModelViewSet): """ return super(ExampleSerializer, self).retrieve(self, request) + @detail_route(methods=['post'], serializer_class=AnotherSerializerWithListFields) + def custom_action_with_list_fields(self, request, pk): + """ + A custom action using both list field and list serializer in the serializer. + """ + return super(ExampleSerializer, self).retrieve(self, request) + @list_route() def custom_list_action(self, request): return super(ExampleViewSet, self).list(self, request) @@ -174,6 +186,17 @@ class TestRouterGeneratedSchema(TestCase): coreapi.Field('d', required=False, location='form', schema=coreschema.String(title='D')), ] ), + 'custom_action_with_list_fields': coreapi.Link( + url='/example/{id}/custom_action_with_list_fields/', + action='post', + encoding='application/json', + description='A custom action using both list field and list serializer in the serializer.', + fields=[ + coreapi.Field('id', required=True, location='path', schema=coreschema.String()), + coreapi.Field('a', required=True, location='form', schema=coreschema.Array(title='A', items=coreschema.Integer())), + coreapi.Field('b', required=True, location='form', schema=coreschema.Array(title='B', items=coreschema.String())), + ] + ), 'custom_list_action': coreapi.Link( url='/example/custom_list_action/', action='get'