Skip to content

Commit

Permalink
Improved handling of nullable Raw fields for OAS 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tsokalski committed Dec 6, 2024
1 parent 6583194 commit 89aeda3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,6 @@ venv.bak/

# ruff
.ruff_cache/

# PyCharm
.idea
2 changes: 1 addition & 1 deletion src/apispec/ext/marshmallow/field_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def field2nullable(self, field: marshmallow.fields.Field, ret) -> dict:
attributes["anyOf"] = [{"$ref": ret.pop("$ref")}, {"type": "null"}]
elif "allOf" in ret:
attributes["anyOf"] = [*ret.pop("allOf"), {"type": "null"}]
else:
elif "type" in ret:
attributes["type"] = [*make_type_list(ret.get("type")), "null"]
return attributes

Expand Down
14 changes: 14 additions & 0 deletions tests/test_ext_marshmallow_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ def test_field_with_allow_none(spec_fixture):
assert res["type"] == ["string", "null"]


@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True)
@pytest.mark.parametrize("field_class", [fields.Field, fields.Raw])
def test_nullable_field_with_no_type(spec_fixture, field_class):
field = field_class(allow_none=True)
res = spec_fixture.openapi.field2property(field)
if spec_fixture.openapi.openapi_version.major < 3:
assert res["x-nullable"] is True
elif spec_fixture.openapi.openapi_version.minor < 1:
assert res["nullable"] is True
else:
assert "nullable" not in res
assert "type" not in res


@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True)
def test_nested_nullable(spec_fixture):
class Child(Schema):
Expand Down

0 comments on commit 89aeda3

Please sign in to comment.