diff --git a/apispec/ext/marshmallow/swagger.py b/apispec/ext/marshmallow/swagger.py index ea977448..3f676a1e 100644 --- a/apispec/ext/marshmallow/swagger.py +++ b/apispec/ext/marshmallow/swagger.py @@ -241,6 +241,9 @@ def field2property(field, spec=None, use_refs=True, dump=True, name=None): if field.dump_only: ret['readOnly'] = True + if field.allow_none: + ret['x-nullable'] = True + ret.update(field2range(field)) ret.update(field2length(field)) diff --git a/tests/test_swagger.py b/tests/test_swagger.py index d31ad4d3..b460b653 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -128,6 +128,10 @@ def test_field_with_additional_metadata(self): assert res['maxLength'] == 100 assert res['minLength'] == 6 + def test_field_with_allow_none(self): + field = fields.Str(allow_none=True) + res = swagger.field2property(field) + assert res['x-nullable'] is True class TestMarshmallowSchemaToModelDefinition: