From e3565eea17bcdaa22a756fe6c1c040bc361e4d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= <16805946+edgarrmondragon@users.noreply.github.com> Date: Thu, 15 Feb 2024 15:11:37 -0600 Subject: [PATCH] fix: Instances of `oneOf` are now handled by null-appending logic (#2245) fix: Instance of `oneOf` are now handled by null-appending logic --- singer_sdk/helpers/_typing.py | 4 ++++ tests/core/test_typing.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/singer_sdk/helpers/_typing.py b/singer_sdk/helpers/_typing.py index 6df38e83b..f3ea68e1b 100644 --- a/singer_sdk/helpers/_typing.py +++ b/singer_sdk/helpers/_typing.py @@ -59,6 +59,10 @@ def append_type(type_dict: dict, new_type: str) -> dict: result["anyOf"] = [result["anyOf"], new_type] return result + if "oneOf" in result: + result["oneOf"].append(new_type) + return result + if "type" in result: type_array = ( result["type"] if isinstance(result["type"], list) else [result["type"]] diff --git a/tests/core/test_typing.py b/tests/core/test_typing.py index 7bb0ab362..b985aeecc 100644 --- a/tests/core/test_typing.py +++ b/tests/core/test_typing.py @@ -19,6 +19,7 @@ PropertiesList, Property, StringType, + append_type, to_sql_type, ) @@ -318,3 +319,33 @@ def test_conform_primitives(): ) def test_to_sql_type(jsonschema_type, expected): assert isinstance(to_sql_type(jsonschema_type), expected) + + +@pytest.mark.parametrize( + "type_dict,expected", + [ + pytest.param({"type": "string"}, {"type": ["string", "null"]}, id="string"), + pytest.param({"type": "integer"}, {"type": ["integer", "null"]}, id="integer"), + pytest.param({"type": "number"}, {"type": ["number", "null"]}, id="number"), + pytest.param({"type": "boolean"}, {"type": ["boolean", "null"]}, id="boolean"), + pytest.param( + {"type": "object", "properties": {}}, + {"type": ["object", "null"], "properties": {}}, + id="object", + ), + pytest.param({"type": "array"}, {"type": ["array", "null"]}, id="array"), + pytest.param( + {"anyOf": [{"type": "integer"}, {"type": "number"}]}, + {"anyOf": [{"type": "integer"}, {"type": "number"}, "null"]}, + id="anyOf", + ), + pytest.param( + {"oneOf": [{"type": "integer"}, {"type": "number"}]}, + {"oneOf": [{"type": "integer"}, {"type": "number"}, "null"]}, + id="oneOf", + ), + ], +) +def test_append_null(type_dict: dict, expected: dict): + result = append_type(type_dict, "null") + assert result == expected