From 96119603e468f7d8911eb2cefee59267e9e54a3d Mon Sep 17 00:00:00 2001 From: Dhaval Mehta Date: Sat, 25 Jan 2020 18:54:08 +0530 Subject: [PATCH] fix boolean case --- rest_framework/schemas/openapi.py | 6 +----- tests/schemas/test_openapi.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 8cf8ee613..eb4528df7 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -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` diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 64baca580..0774c9644 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -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')])),