diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index a284f9b1..63fbe967 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -301,9 +301,13 @@ def field2nullable(self, field: marshmallow.fields.Field, ret) -> dict: if self.openapi_version.major < 3: attributes["x-nullable"] = True elif self.openapi_version.minor < 1: - attributes["nullable"] = True if "$ref" in ret: - attributes["allOf"] = [{"$ref": ret.pop("$ref")}] + attributes["anyOf"] = [ + {"type": "object", "nullable": True}, + {"$ref": ret.pop("$ref")}, + ] + else: + attributes["nullable"] = True else: if "$ref" in ret: attributes["anyOf"] = [{"$ref": ret.pop("$ref")}, {"type": "null"}] @@ -580,9 +584,11 @@ def datetime2properties(self, field, **kwargs: typing.Any) -> dict: ret = { "type": "string", "format": None, - "pattern": field.metadata["pattern"] - if field.metadata.get("pattern") - else None, + "pattern": ( + field.metadata["pattern"] + if field.metadata.get("pattern") + else None + ), } return ret diff --git a/tests/test_ext_marshmallow_field.py b/tests/test_ext_marshmallow_field.py index 20e079d2..5e5250b0 100644 --- a/tests/test_ext_marshmallow_field.py +++ b/tests/test_ext_marshmallow_field.py @@ -211,8 +211,10 @@ class Child(Schema): assert res == {"$ref": "#/definitions/Child", "x-nullable": True} elif version.major == 3 and version.minor < 1: assert res == { - "nullable": True, - "allOf": [{"$ref": "#/components/schemas/Child"}], + "anyOf": [ + {"type": "object", "nullable": True}, + {"$ref": "#/components/schemas/Child"}, + ] } else: assert res == {