diff --git a/apispec/ext/marshmallow/swagger.py b/apispec/ext/marshmallow/swagger.py index f0e3458d..c74ff103 100644 --- a/apispec/ext/marshmallow/swagger.py +++ b/apispec/ext/marshmallow/swagger.py @@ -216,6 +216,23 @@ def field2length(field): return attributes +def field2nullable(field, spec): + """Returns the dictionary with an appropriate nullable key depending on + the OpenAPI version. Returns an empty dict if the field is not nullable + + :param Field field: A marshmallow field. + :param spec: apispec.core.APISpec instance + :rtype: dict + """ + if not field.allow_none: + return {} + + if spec and spec.openapi_version.version[0] == 3: + return {'nullable': True} + + return {'x-nullable': True} + + # Properties that may be defined in a field's metadata that will be added to the output # of field2property # https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject @@ -293,8 +310,7 @@ 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(field2nullable(field, spec)) ret.update(field2range(field)) ret.update(field2length(field)) diff --git a/tests/test_swagger.py b/tests/test_swagger.py index 0e3795d3..6983dfc4 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -203,6 +203,18 @@ def test_field_with_allow_none(self): res = swagger.field2property(field) assert res['x-nullable'] is True + def test_field_with_allow_none_v3(self): + spec = APISpec( + title='Pets', + version='0.1', + plugins=['apispec.ext.marshmallow'], + openapi_version='3.0.0' + ) + field = fields.Str(allow_none=True) + res = swagger.field2property(field, spec) + assert res['nullable'] is True + + class TestMarshmallowSchemaToModelDefinition: def test_invalid_schema(self):