From 89aeda3e19b690139f88b4b81f43902bb03eb0a2 Mon Sep 17 00:00:00 2001 From: Tayler Sokalski Date: Fri, 6 Dec 2024 15:37:52 -0800 Subject: [PATCH 1/2] Improved handling of nullable Raw fields for OAS 3.1.0 --- .gitignore | 3 +++ src/apispec/ext/marshmallow/field_converter.py | 2 +- tests/test_ext_marshmallow_field.py | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8ba96d31..21d9cbc7 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,6 @@ venv.bak/ # ruff .ruff_cache/ + +# PyCharm +.idea diff --git a/src/apispec/ext/marshmallow/field_converter.py b/src/apispec/ext/marshmallow/field_converter.py index 3b837b44..9016e5f5 100644 --- a/src/apispec/ext/marshmallow/field_converter.py +++ b/src/apispec/ext/marshmallow/field_converter.py @@ -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 diff --git a/tests/test_ext_marshmallow_field.py b/tests/test_ext_marshmallow_field.py index 36d864c4..a24f0a3f 100644 --- a/tests/test_ext_marshmallow_field.py +++ b/tests/test_ext_marshmallow_field.py @@ -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): From 968935ba34e1f056de1c6b05d645e6c535bf0d4c Mon Sep 17 00:00:00 2001 From: Tayler Sokalski Date: Tue, 10 Dec 2024 18:12:24 -0500 Subject: [PATCH 2/2] Remove .gitignore entry --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 21d9cbc7..8ba96d31 100644 --- a/.gitignore +++ b/.gitignore @@ -106,6 +106,3 @@ venv.bak/ # ruff .ruff_cache/ - -# PyCharm -.idea