Added mapping of ListField.child type for OpenAPI Schemas.

This commit is contained in:
Daniel Alvarez 2019-07-20 22:32:15 +02:00 committed by Carlton Gibson
parent f7c3220fdb
commit 2138f558ce
2 changed files with 22 additions and 2 deletions

View File

@ -9,7 +9,7 @@ from django.utils.encoding import force_text
from rest_framework import exceptions, serializers
from rest_framework.compat import uritemplate
from rest_framework.fields import empty
from rest_framework.fields import _UnvalidatedField, empty
from .generators import BaseSchemaGenerator
from .inspectors import ViewInspector
@ -256,9 +256,15 @@ class AutoSchema(ViewInspector):
# ListField.
if isinstance(field, serializers.ListField):
return {
mapping = {
'type': 'array',
'items': {},
}
if not isinstance(field.child, _UnvalidatedField):
mapping['items'] = {
"type": self._map_field(field.child).get('type')
}
return mapping
# DateField and DateTimeField type is string
if isinstance(field, serializers.DateField):

View File

@ -39,6 +39,20 @@ class TestBasics(TestCase):
assert f.get_schema_operation_parameters(self.dummy_view)
class TestFieldMapping(TestCase):
def test_list_field_mapping(self):
inspector = AutoSchema()
cases = [
(serializers.ListField(), {'items': {}, 'type': 'array'}),
(serializers.ListField(child=serializers.BooleanField()), {'items': {'type': 'boolean'}, 'type': 'array'}),
(serializers.ListField(child=serializers.FloatField()), {'items': {'type': 'number'}, 'type': 'array'}),
(serializers.ListField(child=serializers.CharField()), {'items': {'type': 'string'}, 'type': 'array'}),
]
for field, mapping in cases:
with self.subTest(field=field):
assert inspector._map_field(field) == mapping
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
class TestOperationIntrospection(TestCase):