Skip to content

Commit

Permalink
Set x-nullable based on allow_null (#217)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Rémi Lapeyre authored and axnsan12 committed Oct 9, 2018
1 parent c510de1 commit d41f0c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/drf_yasg/inspectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def SwaggerType(existing_object=None, **instance_kwargs):
- arguments specified by the ``kwargs`` parameter of :meth:`._get_partial_types`
- ``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
If ``existing_object`` is not ``None``, it is updated instead of creating a new object.
Expand Down Expand Up @@ -225,6 +225,9 @@ def SwaggerType(existing_object=None, **instance_kwargs):
instance_kwargs.setdefault('title', title)
if description is not None:
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)

if existing_object is not None:
Expand Down
7 changes: 6 additions & 1 deletion testproj/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ class Meta:


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(
child=serializers.ChoiceField(choices=['wide', 'tall', 'thumb', 'social']),
help_text="Parameter with Items"
Expand Down
7 changes: 7 additions & 0 deletions tests/reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ paths:
pattern: ^69$
default: '69'
minLength: 1
x-nullable: true
- name: image_styles
in: formData
description: Parameter with Items
Expand Down Expand Up @@ -885,6 +886,7 @@ definitions:
- 3
- 7
- 8
x-nullable: true
group:
type: string
format: uuid
Expand All @@ -903,6 +905,7 @@ definitions:
pattern: ^69$
default: '69'
minLength: 1
x-nullable: true
image_styles:
description: Parameter with Items
type: array
Expand Down Expand Up @@ -931,12 +934,14 @@ definitions:
type: string
maxLength: 30
minLength: 1
x-nullable: true
last_name:
title: Last name
description: <strong>Here's some HTML!</strong>
type: string
maxLength: 30
minLength: 1
x-nullable: true
Person:
required:
- identity
Expand Down Expand Up @@ -1488,6 +1493,7 @@ definitions:
type: array
items:
type: integer
x-nullable: true
exampleProjects:
type: array
items:
Expand Down Expand Up @@ -1552,6 +1558,7 @@ definitions:
parent_id:
type: integer
title: Parent id
x-nullable: true
TodoTree:
required:
- title
Expand Down

0 comments on commit d41f0c5

Please sign in to comment.