Skip to content

Commit a8d129b

Browse files
jlainecarltongibson
authored andcommitted
Represent serializer DictField as an Object in schema
DictFields were incorrectly being output as String in the schema. This pull request outputs an Object instead and adds a unit test. Update s/detail_route/action/ after rebase
1 parent 27f32fa commit a8d129b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

rest_framework/schemas/inspectors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def field_to_schema(field):
3434
title=title,
3535
description=description
3636
)
37+
elif isinstance(field, serializers.DictField):
38+
return coreschema.Object(
39+
title=title,
40+
description=description
41+
)
3742
elif isinstance(field, serializers.Serializer):
3843
return coreschema.Object(
3944
properties=OrderedDict([

tests/test_schemas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class ExampleSerializer(serializers.Serializer):
4949
hidden = serializers.HiddenField(default='hello')
5050

5151

52+
class AnotherSerializerWithDictField(serializers.Serializer):
53+
a = serializers.DictField()
54+
55+
5256
class AnotherSerializerWithListFields(serializers.Serializer):
5357
a = serializers.ListField(child=serializers.IntegerField())
5458
b = serializers.ListSerializer(child=serializers.CharField())
@@ -72,6 +76,13 @@ def custom_action(self, request, pk):
7276
"""
7377
return super(ExampleSerializer, self).retrieve(self, request)
7478

79+
@action(methods=['post'], detail=True, serializer_class=AnotherSerializerWithDictField)
80+
def custom_action_with_dict_field(self, request, pk):
81+
"""
82+
A custom action using a dict field in the serializer.
83+
"""
84+
return super(ExampleSerializer, self).retrieve(self, request)
85+
7586
@action(methods=['post'], detail=True, serializer_class=AnotherSerializerWithListFields)
7687
def custom_action_with_list_fields(self, request, pk):
7788
"""
@@ -198,6 +209,16 @@ def test_authenticated_request(self):
198209
coreapi.Field('d', required=False, location='form', schema=coreschema.String(title='D')),
199210
]
200211
),
212+
'custom_action_with_dict_field': coreapi.Link(
213+
url='/example/{id}/custom_action_with_dict_field/',
214+
action='post',
215+
encoding='application/json',
216+
description='A custom action using a dict field in the serializer.',
217+
fields=[
218+
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
219+
coreapi.Field('a', required=True, location='form', schema=coreschema.Object(title='A')),
220+
]
221+
),
201222
'custom_action_with_list_fields': coreapi.Link(
202223
url='/example/{id}/custom_action_with_list_fields/',
203224
action='post',

0 commit comments

Comments
 (0)