mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-25 07:50:41 +03:00
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
This commit is contained in:
parent
27f32faee4
commit
a8d129b7da
|
@ -34,6 +34,11 @@ def field_to_schema(field):
|
|||
title=title,
|
||||
description=description
|
||||
)
|
||||
elif isinstance(field, serializers.DictField):
|
||||
return coreschema.Object(
|
||||
title=title,
|
||||
description=description
|
||||
)
|
||||
elif isinstance(field, serializers.Serializer):
|
||||
return coreschema.Object(
|
||||
properties=OrderedDict([
|
||||
|
|
|
@ -49,6 +49,10 @@ class ExampleSerializer(serializers.Serializer):
|
|||
hidden = serializers.HiddenField(default='hello')
|
||||
|
||||
|
||||
class AnotherSerializerWithDictField(serializers.Serializer):
|
||||
a = serializers.DictField()
|
||||
|
||||
|
||||
class AnotherSerializerWithListFields(serializers.Serializer):
|
||||
a = serializers.ListField(child=serializers.IntegerField())
|
||||
b = serializers.ListSerializer(child=serializers.CharField())
|
||||
|
@ -72,6 +76,13 @@ class ExampleViewSet(ModelViewSet):
|
|||
"""
|
||||
return super(ExampleSerializer, self).retrieve(self, request)
|
||||
|
||||
@action(methods=['post'], detail=True, serializer_class=AnotherSerializerWithDictField)
|
||||
def custom_action_with_dict_field(self, request, pk):
|
||||
"""
|
||||
A custom action using a dict field in the serializer.
|
||||
"""
|
||||
return super(ExampleSerializer, self).retrieve(self, request)
|
||||
|
||||
@action(methods=['post'], detail=True, serializer_class=AnotherSerializerWithListFields)
|
||||
def custom_action_with_list_fields(self, request, pk):
|
||||
"""
|
||||
|
@ -198,6 +209,16 @@ class TestRouterGeneratedSchema(TestCase):
|
|||
coreapi.Field('d', required=False, location='form', schema=coreschema.String(title='D')),
|
||||
]
|
||||
),
|
||||
'custom_action_with_dict_field': coreapi.Link(
|
||||
url='/example/{id}/custom_action_with_dict_field/',
|
||||
action='post',
|
||||
encoding='application/json',
|
||||
description='A custom action using a dict field in the serializer.',
|
||||
fields=[
|
||||
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
|
||||
coreapi.Field('a', required=True, location='form', schema=coreschema.Object(title='A')),
|
||||
]
|
||||
),
|
||||
'custom_action_with_list_fields': coreapi.Link(
|
||||
url='/example/{id}/custom_action_with_list_fields/',
|
||||
action='post',
|
||||
|
|
Loading…
Reference in New Issue
Block a user