mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-28 17:09:59 +03:00
fix boolean case
This commit is contained in:
parent
0a6322e7d5
commit
96119603e4
|
@ -214,11 +214,7 @@ class AutoSchema(ViewInspector):
|
||||||
def _map_choicefield(self, field):
|
def _map_choicefield(self, field):
|
||||||
choices = list(field.choices)
|
choices = list(field.choices)
|
||||||
if all(isinstance(choice, bool) for choice in 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
|
type = 'boolean'
|
||||||
# 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))
|
|
||||||
elif all(isinstance(choice, int) for choice in choices):
|
elif all(isinstance(choice, int) for choice in choices):
|
||||||
type = 'integer'
|
type = 'integer'
|
||||||
elif all(isinstance(choice, (int, float, Decimal)) for choice in choices): # `number` includes `integer`
|
elif all(isinstance(choice, (int, float, Decimal)) for choice in choices): # `number` includes `integer`
|
||||||
|
|
|
@ -63,7 +63,7 @@ class TestFieldMapping(TestCase):
|
||||||
(serializers.ListField(child=serializers.ChoiceField(choices=[(1.1, 'First'), (2.2, 'Second')])),
|
(serializers.ListField(child=serializers.ChoiceField(choices=[(1.1, 'First'), (2.2, 'Second')])),
|
||||||
{'items': {'enum': [1.1, 2.2], 'type': 'number'}, 'type': 'array'}),
|
{'items': {'enum': [1.1, 2.2], 'type': 'number'}, 'type': 'array'}),
|
||||||
(serializers.ListField(child=serializers.ChoiceField(choices=[(True, 'true'), (False, 'false')])),
|
(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')])),
|
(serializers.ListField(child=serializers.ChoiceField(choices=[(uuid1, 'uuid1'), (uuid2, 'uuid2')])),
|
||||||
{'items': {'enum': [uuid1, uuid2]}, 'type': 'array'}),
|
{'items': {'enum': [uuid1, uuid2]}, 'type': 'array'}),
|
||||||
(serializers.ListField(child=serializers.ChoiceField(choices=[(1, 'One'), ('a', 'Choice A')])),
|
(serializers.ListField(child=serializers.ChoiceField(choices=[(1, 'One'), ('a', 'Choice A')])),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user