From d41f0c5ac4b771b25bd07630923385b938905db7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Lapeyre?= Date: Wed, 10 Oct 2018 00:24:42 +0200 Subject: [PATCH] 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. --- src/drf_yasg/inspectors/base.py | 5 ++++- testproj/articles/serializers.py | 7 ++++++- tests/reference.yaml | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/drf_yasg/inspectors/base.py b/src/drf_yasg/inspectors/base.py index ef9972e..ff3c870 100644 --- a/src/drf_yasg/inspectors/base.py +++ b/src/drf_yasg/inspectors/base.py @@ -192,7 +192,7 @@ class FieldInspector(BaseInspector): - 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. @@ -225,6 +225,9 @@ class FieldInspector(BaseInspector): 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: diff --git a/testproj/articles/serializers.py b/testproj/articles/serializers.py index e4804a4..a46c653 100644 --- a/testproj/articles/serializers.py +++ b/testproj/articles/serializers.py @@ -33,7 +33,12 @@ class ArticleSerializer(serializers.ModelSerializer): 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" diff --git a/tests/reference.yaml b/tests/reference.yaml index d7a6df8..545c7d4 100644 --- a/tests/reference.yaml +++ b/tests/reference.yaml @@ -207,6 +207,7 @@ paths: pattern: ^69$ default: '69' minLength: 1 + x-nullable: true - name: image_styles in: formData description: Parameter with Items @@ -885,6 +886,7 @@ definitions: - 3 - 7 - 8 + x-nullable: true group: type: string format: uuid @@ -903,6 +905,7 @@ definitions: pattern: ^69$ default: '69' minLength: 1 + x-nullable: true image_styles: description: Parameter with Items type: array @@ -931,12 +934,14 @@ definitions: type: string maxLength: 30 minLength: 1 + x-nullable: true last_name: title: Last name description: Here's some HTML! type: string maxLength: 30 minLength: 1 + x-nullable: true Person: required: - identity @@ -1488,6 +1493,7 @@ definitions: type: array items: type: integer + x-nullable: true exampleProjects: type: array items: @@ -1552,6 +1558,7 @@ definitions: parent_id: type: integer title: Parent id + x-nullable: true TodoTree: required: - title