Set x-nullable based on allow_null (#217)

Many fields may be set a nullable in an API. While not covered explicitly by Swagger 2, this information is usually indicated as a [vendor extension](https://swagger.io/docs/specification/2-0/swagger-extensions/) using the x-nullable field.
master
Rémi Lapeyre 2018-10-10 00:24:42 +02:00 committed by Cristi Vîjdea
parent c510de13d7
commit d41f0c5ac4
3 changed files with 17 additions and 2 deletions

View File

@ -192,7 +192,7 @@ class FieldInspector(BaseInspector):
- arguments specified by the ``kwargs`` parameter of :meth:`._get_partial_types` - arguments specified by the ``kwargs`` parameter of :meth:`._get_partial_types`
- ``instance_kwargs`` passed to the constructor function - ``instance_kwargs`` passed to the constructor function
- ``title``, ``description``, ``required`` and ``default`` inferred from the field, - ``title``, ``description``, ``required``, ``x-nullable`` and ``default`` inferred from the field,
where appropriate where appropriate
If ``existing_object`` is not ``None``, it is updated instead of creating a new object. If ``existing_object`` is not ``None``, it is updated instead of creating a new object.
@ -225,6 +225,9 @@ class FieldInspector(BaseInspector):
instance_kwargs.setdefault('title', title) instance_kwargs.setdefault('title', title)
if description is not None: if description is not None:
instance_kwargs.setdefault('description', description) instance_kwargs.setdefault('description', description)
if field.allow_null and not instance_kwargs.get('required', False) and not field.required:
instance_kwargs['x_nullable'] = True
instance_kwargs.update(kwargs) instance_kwargs.update(kwargs)
if existing_object is not None: if existing_object is not None:

View File

@ -33,7 +33,12 @@ class ArticleSerializer(serializers.ModelSerializer):
class ImageUploadSerializer(serializers.Serializer): class ImageUploadSerializer(serializers.Serializer):
what_am_i_doing = serializers.RegexField(regex=r"^69$", help_text="test", default="69") what_am_i_doing = serializers.RegexField(
regex=r"^69$",
help_text="test",
default="69",
allow_null=True
)
image_styles = serializers.ListSerializer( image_styles = serializers.ListSerializer(
child=serializers.ChoiceField(choices=['wide', 'tall', 'thumb', 'social']), child=serializers.ChoiceField(choices=['wide', 'tall', 'thumb', 'social']),
help_text="Parameter with Items" help_text="Parameter with Items"

View File

@ -207,6 +207,7 @@ paths:
pattern: ^69$ pattern: ^69$
default: '69' default: '69'
minLength: 1 minLength: 1
x-nullable: true
- name: image_styles - name: image_styles
in: formData in: formData
description: Parameter with Items description: Parameter with Items
@ -885,6 +886,7 @@ definitions:
- 3 - 3
- 7 - 7
- 8 - 8
x-nullable: true
group: group:
type: string type: string
format: uuid format: uuid
@ -903,6 +905,7 @@ definitions:
pattern: ^69$ pattern: ^69$
default: '69' default: '69'
minLength: 1 minLength: 1
x-nullable: true
image_styles: image_styles:
description: Parameter with Items description: Parameter with Items
type: array type: array
@ -931,12 +934,14 @@ definitions:
type: string type: string
maxLength: 30 maxLength: 30
minLength: 1 minLength: 1
x-nullable: true
last_name: last_name:
title: Last name title: Last name
description: <strong>Here's some HTML!</strong> description: <strong>Here's some HTML!</strong>
type: string type: string
maxLength: 30 maxLength: 30
minLength: 1 minLength: 1
x-nullable: true
Person: Person:
required: required:
- identity - identity
@ -1488,6 +1493,7 @@ definitions:
type: array type: array
items: items:
type: integer type: integer
x-nullable: true
exampleProjects: exampleProjects:
type: array type: array
items: items:
@ -1552,6 +1558,7 @@ definitions:
parent_id: parent_id:
type: integer type: integer
title: Parent id title: Parent id
x-nullable: true
TodoTree: TodoTree:
required: required:
- title - title