fix boolean case

This commit is contained in:
Dhaval Mehta 2020-01-25 18:54:08 +05:30
parent 0a6322e7d5
commit 96119603e4
2 changed files with 2 additions and 6 deletions

View File

@ -214,11 +214,7 @@ class AutoSchema(ViewInspector):
def _map_choicefield(self, field):
choices = list(field.choices)
if all(isinstance(choice, bool) for choice in choices):
# Here we can not use `boolean` as type because choicefield expects `True` and `False` which is different
# from the `true` and `false` of OpenAPI Specification.
# Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#format
type = 'string'
choices = list(map(lambda choice: str(choice), choices))
type = 'boolean'
elif all(isinstance(choice, int) for choice in choices):
type = 'integer'
elif all(isinstance(choice, (int, float, Decimal)) for choice in choices): # `number` includes `integer`

View File

@ -63,7 +63,7 @@ class TestFieldMapping(TestCase):
(serializers.ListField(child=serializers.ChoiceField(choices=[(1.1, 'First'), (2.2, 'Second')])),
{'items': {'enum': [1.1, 2.2], 'type': 'number'}, 'type': 'array'}),
(serializers.ListField(child=serializers.ChoiceField(choices=[(True, 'true'), (False, 'false')])),
{'items': {'enum': ['True', 'False'], 'type': 'string'}, 'type': 'array'}),
{'items': {'enum': [True, False], 'type': 'boolean'}, 'type': 'array'}),
(serializers.ListField(child=serializers.ChoiceField(choices=[(uuid1, 'uuid1'), (uuid2, 'uuid2')])),
{'items': {'enum': [uuid1, uuid2]}, 'type': 'array'}),
(serializers.ListField(child=serializers.ChoiceField(choices=[(1, 'One'), ('a', 'Choice A')])),