OpenAPI: Use int64 format for large integers. (#7018)

This commit is contained in:
Kentalot 2019-11-06 12:46:19 -08:00 committed by Carlton Gibson
parent 8b06ce72d7
commit becb962160
2 changed files with 13 additions and 2 deletions

View File

@ -268,9 +268,13 @@ class AutoSchema(ViewInspector):
'items': {}, 'items': {},
} }
if not isinstance(field.child, _UnvalidatedField): if not isinstance(field.child, _UnvalidatedField):
mapping['items'] = { map_field = self._map_field(field.child)
"type": self._map_field(field.child).get('type') items = {
"type": map_field.get('type')
} }
if 'format' in map_field:
items['format'] = map_field.get('format')
mapping['items'] = items
return mapping return mapping
# DateField and DateTimeField type is string # DateField and DateTimeField type is string
@ -340,6 +344,9 @@ class AutoSchema(ViewInspector):
'type': 'integer' 'type': 'integer'
} }
self._map_min_max(field, content) self._map_min_max(field, content)
# 2147483647 is max for int32_size, so we use int64 for format
if int(content.get('maximum', 0)) > 2147483647 or int(content.get('minimum', 0)) > 2147483647:
content['format'] = 'int64'
return content return content
if isinstance(field, serializers.FileField): if isinstance(field, serializers.FileField):

View File

@ -50,6 +50,10 @@ class TestFieldMapping(TestCase):
(serializers.ListField(child=serializers.BooleanField()), {'items': {'type': 'boolean'}, '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.FloatField()), {'items': {'type': 'number'}, 'type': 'array'}),
(serializers.ListField(child=serializers.CharField()), {'items': {'type': 'string'}, 'type': 'array'}), (serializers.ListField(child=serializers.CharField()), {'items': {'type': 'string'}, 'type': 'array'}),
(serializers.ListField(child=serializers.IntegerField(max_value=4294967295)),
{'items': {'type': 'integer', 'format': 'int64'}, 'type': 'array'}),
(serializers.IntegerField(min_value=2147483648),
{'type': 'integer', 'minimum': 2147483648, 'format': 'int64'}),
] ]
for field, mapping in cases: for field, mapping in cases:
with self.subTest(field=field): with self.subTest(field=field):