Use collectionFormat=multi for MultiChoiceField in query and formData

Fixes #257.
master
Cristi Vîjdea 2018-11-29 01:38:32 +02:00
parent 9f60dc191a
commit 708e70a526
3 changed files with 26 additions and 3 deletions

View File

@ -597,16 +597,22 @@ class ChoiceFieldInspector(FieldInspector):
if model_type: if model_type:
enum_type = model_type.get('type', enum_type) enum_type = model_type.get('type', enum_type)
enum_values = list(field.choices.keys())
if isinstance(field, serializers.MultipleChoiceField): if isinstance(field, serializers.MultipleChoiceField):
return SwaggerType( result = SwaggerType(
type=openapi.TYPE_ARRAY, type=openapi.TYPE_ARRAY,
items=ChildSwaggerType( items=ChildSwaggerType(
type=enum_type, type=enum_type,
enum=list(field.choices.keys()) enum=enum_values
) )
) )
if swagger_object_type == openapi.Parameter:
if result['in'] in (openapi.IN_FORM, openapi.IN_QUERY):
result.collection_format = 'multi'
else:
result = SwaggerType(type=enum_type, enum=enum_values)
return SwaggerType(type=enum_type, enum=list(field.choices.keys())) return result
return NotHandled return NotHandled

View File

@ -81,3 +81,4 @@ class UserSerializerrr(serializers.ModelSerializer):
class UserListQuerySerializer(serializers.Serializer): class UserListQuerySerializer(serializers.Serializer):
username = serializers.CharField(help_text="this field is generated from a query_serializer", required=False) username = serializers.CharField(help_text="this field is generated from a query_serializer", required=False)
is_staff = serializers.BooleanField(help_text="this one too!", required=False) is_staff = serializers.BooleanField(help_text="this one too!", required=False)
styles = serializers.MultipleChoiceField(help_text="and this one is fancy!", choices=('a', 'b', 'c', 'd'))

View File

@ -740,6 +740,19 @@ paths:
description: this one too! description: this one too!
required: false required: false
type: boolean type: boolean
- name: styles
in: query
description: and this one is fancy!
required: true
type: array
items:
type: string
enum:
- a
- b
- c
- d
collectionFormat: multi
responses: responses:
'200': '200':
description: '' description: ''
@ -1159,6 +1172,7 @@ definitions:
- fan - fan
- fancy - fancy
- felix - felix
- fennel
- fish - fish
- flatline - flatline
- forth - forth
@ -1184,6 +1198,7 @@ definitions:
- haskell - haskell
- haxeml - haxeml
- hexdump - hexdump
- hlsl
- hsail - hsail
- html - html
- html+cheetah - html+cheetah
@ -1450,6 +1465,7 @@ definitions:
- xml+php - xml+php
- xml+smarty - xml+smarty
- xml+velocity - xml+velocity
- xorg.conf
- xquery - xquery
- xslt - xslt
- xtend - xtend