Skip to content

Commit 0924569

Browse files
tsokalskisloria
andauthored
Improve handling of nullable Raw fields for OAS 3.1.0 (#961)
* 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>
1 parent 59751b7 commit 0924569

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
Changelog
22
---------
33

4-
6.9.0 (unreleased)
4+
6.8.1 (unreleased)
55
******************
66

7+
Bug fixes:
8+
9+
- Fix handling of nullable Raw fields for OAS 3.1.0 (:issue:`960`).
10+
Thanks :user:`tsokalski` for reporting and fixing.
11+
712
Support:
813

914
- Support marshmallow 4 (:pr:`963`).

src/apispec/ext/marshmallow/field_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def field2nullable(self, field: marshmallow.fields.Field, ret) -> dict:
318318
attributes["anyOf"] = [{"$ref": ret.pop("$ref")}, {"type": "null"}]
319319
elif "allOf" in ret:
320320
attributes["anyOf"] = [*ret.pop("allOf"), {"type": "null"}]
321-
else:
321+
elif "type" in ret:
322322
attributes["type"] = [*make_type_list(ret.get("type")), "null"]
323323
return attributes
324324

tests/test_ext_marshmallow_field.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ def test_field_with_allow_none(spec_fixture):
197197
assert res["type"] == ["string", "null"]
198198

199199

200+
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True)
201+
@pytest.mark.parametrize("field_class", [fields.Field, fields.Raw])
202+
def test_nullable_field_with_no_type(spec_fixture, field_class):
203+
field = field_class(allow_none=True)
204+
res = spec_fixture.openapi.field2property(field)
205+
if spec_fixture.openapi.openapi_version.major < 3:
206+
assert res["x-nullable"] is True
207+
elif spec_fixture.openapi.openapi_version.minor < 1:
208+
assert res["nullable"] is True
209+
else:
210+
assert "nullable" not in res
211+
assert "type" not in res
212+
213+
200214
@pytest.mark.parametrize("spec_fixture", ("2.0", "3.0.0", "3.1.0"), indirect=True)
201215
def test_nested_nullable(spec_fixture):
202216
class Child(Schema):

0 commit comments

Comments
 (0)