Skip to content

Commit

Permalink
Improve handling of nullable Raw fields for OAS 3.1.0 (#961)
Browse files Browse the repository at this point in the history
* Improved handling of nullable Raw fields for OAS 3.1.0

* Remove .gitignore entry

* Update changelog

---------

Co-authored-by: Tayler Sokalski <tsokalski@users.noreply.github.com>
Co-authored-by: Steven Loria <sloria1@gmail.com>
  • Loading branch information
3 people authored Jan 7, 2025
1 parent 59751b7 commit 0924569
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Changelog
---------

6.9.0 (unreleased)
6.8.1 (unreleased)
******************

Bug fixes:

- Fix handling of nullable Raw fields for OAS 3.1.0 (:issue:`960`).
Thanks :user:`tsokalski` for reporting and fixing.

Support:

- Support marshmallow 4 (:pr:`963`).
Expand Down
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 @@ -197,6 +197,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 0924569

Please sign in to comment.