Skip to content

Commit

Permalink
Merge pull request #953 from ShepleySound/rshepley/nested-nullable-3.0
Browse files Browse the repository at this point in the history
fix: Nullable nested fields comply with 3.0 spec
  • Loading branch information
lafrech authored Nov 4, 2024
2 parents d9507a4 + 0d9a841 commit e555f45
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/apispec/ext/marshmallow/field_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]
Expand Down Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions tests/test_ext_marshmallow_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == {
Expand Down

0 comments on commit e555f45

Please sign in to comment.