Add enum type inference based on choices values (#264)

This commit is contained in:
Alexander Egorov
2018-12-07 19:11:13 +07:00
committed by Cristi Vîjdea
parent f587785eb4
commit f6544654ab
2 changed files with 37 additions and 1 deletions
+8 -1
View File
@@ -586,6 +586,7 @@ class ChoiceFieldInspector(FieldInspector):
if isinstance(field, serializers.ChoiceField):
enum_type = openapi.TYPE_STRING
enum_values = list(field.choices.keys())
# for ModelSerializer, try to infer the type from the associated model field
serializer = get_parent_serializer(field)
@@ -596,8 +597,14 @@ class ChoiceFieldInspector(FieldInspector):
model_type = get_basic_type_info(model_field)
if model_type:
enum_type = model_type.get('type', enum_type)
else:
# Try to infer field type based on enum values
enum_value_types = {type(v) for v in enum_values}
if len(enum_value_types) == 1:
values_type = get_basic_type_info_from_hint(next(iter(enum_value_types)))
if values_type:
enum_type = values_type.get('type', enum_type)
enum_values = list(field.choices.keys())
if isinstance(field, serializers.MultipleChoiceField):
result = SwaggerType(
type=openapi.TYPE_ARRAY,