From 7fbbfb0ec9f96f079d7e072a0d9bbdc767c22409 Mon Sep 17 00:00:00 2001 From: Dhaval Mehta Date: Sat, 25 Jan 2020 11:06:47 +0530 Subject: [PATCH] add type to choicefield --- rest_framework/schemas/openapi.py | 39 ++++++++++++++++++++++++++----- tests/schemas/test_openapi.py | 16 ++++++++++++- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 3a7eb29a7..5c81c7ed3 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -1,4 +1,6 @@ import warnings +from collections import OrderedDict +from decimal import Decimal from operator import attrgetter from urllib.parse import urljoin @@ -209,6 +211,35 @@ class AutoSchema(ViewInspector): return paginator.get_schema_operation_parameters(view) + def _map_choicefield(self, field): + type = None + choices = list(field.choices) + if all(isinstance(choice, int) for choice in choices): + type = 'integer' + if all(isinstance(choice, float) or isinstance(choice, Decimal) for choice in choices): + type = 'number' + if all(isinstance(choice, str) for choice in choices): + type = 'string' + 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)) + + mapping = { + # The value of `enum` keyword MUST be an array and SHOULD be unique. + # Ref: https://tools.ietf.org/html/draft-wright-json-schema-validation-00#section-5.20 + 'enum': list(OrderedDict.fromkeys(choices)) # preserve order and remove duplicates + } + + # If We figured out `type` then and only then we should set it. It must be string or an array. + # It is optional but it can not be null. + # Ref: https://tools.ietf.org/html/draft-wright-json-schema-validation-00#section-5.21 + if type: + mapping['type'] = type + return mapping + def _map_field(self, field): # Nested Serializers, `many` or not. @@ -242,15 +273,11 @@ class AutoSchema(ViewInspector): if isinstance(field, serializers.MultipleChoiceField): return { 'type': 'array', - 'items': { - 'enum': list(field.choices) - }, + 'items': self._map_choicefield(field) } if isinstance(field, serializers.ChoiceField): - return { - 'enum': list(field.choices), - } + return self._map_choicefield(field) # ListField. if isinstance(field, serializers.ListField): diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index f734fd169..4ab76a649 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -1,3 +1,5 @@ +import uuid + import pytest from django.conf.urls import url from django.test import RequestFactory, TestCase, override_settings @@ -44,6 +46,8 @@ class TestBasics(TestCase): class TestFieldMapping(TestCase): def test_list_field_mapping(self): + uuid1 = uuid.uuid4() + uuid2 = uuid.uuid4() inspector = AutoSchema() cases = [ (serializers.ListField(), {'items': {}, 'type': 'array'}), @@ -53,7 +57,17 @@ class TestFieldMapping(TestCase): (serializers.ListField(child=serializers.IntegerField(max_value=4294967295)), {'items': {'type': 'integer', 'maximum': 4294967295, 'format': 'int64'}, 'type': 'array'}), (serializers.ListField(child=serializers.ChoiceField(choices=[('a', 'Choice A'), ('b', 'Choice B')])), - {'items': {'enum': ['a', 'b']}, 'type': 'array'}), + {'items': {'enum': ['a', 'b'], 'type': 'string'}, 'type': 'array'}), + (serializers.ListField(child=serializers.ChoiceField(choices=[(1, 'One'), (2, 'Two')])), + {'items': {'enum': [1, 2], 'type': 'integer'}, 'type': 'array'}), + (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'}), + (serializers.ListField(child=serializers.ChoiceField(choices=[(True, 'true'), (False, 'false')])), + {'items': {'enum': ['True', 'False'], 'type': 'string'}, 'type': 'array'}), + (serializers.ListField(child=serializers.ChoiceField(choices=[(uuid1, 'uuid1'), (uuid2, 'uuid2')])), + {'items': {'enum': [uuid1, uuid2]}, 'type': 'array'}), (serializers.IntegerField(min_value=2147483648), {'type': 'integer', 'minimum': 2147483648, 'format': 'int64'}), ]