From fb9ac30603a0f71964f2bde1be57be778268fcc3 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: Wed, 23 Oct 2024 08:21:32 -0600 Subject: [PATCH] fix: Object and array JSON types are now handled before primitive types when converting them to SQL types (#2727) * Add failing test * Prioritize object and array --- singer_sdk/typing.py | 12 ++++++------ tests/core/test_typing.py | 4 ++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/singer_sdk/typing.py b/singer_sdk/typing.py index 845f1dcd0..a21447a49 100644 --- a/singer_sdk/typing.py +++ b/singer_sdk/typing.py @@ -1216,6 +1216,12 @@ def to_sql_type( # noqa: PLR0911, C901 Returns: The SQL type. """ + if _jsonschema_type_check(jsonschema_type, ("object",)): + return sa.types.VARCHAR() + + if _jsonschema_type_check(jsonschema_type, ("array",)): + return sa.types.VARCHAR() + if _jsonschema_type_check(jsonschema_type, ("string",)): datelike_type = get_datelike_property_type(jsonschema_type) if datelike_type: @@ -1236,10 +1242,4 @@ def to_sql_type( # noqa: PLR0911, C901 if _jsonschema_type_check(jsonschema_type, ("boolean",)): return sa.types.BOOLEAN() - if _jsonschema_type_check(jsonschema_type, ("object",)): - return sa.types.VARCHAR() - - if _jsonschema_type_check(jsonschema_type, ("array",)): - return sa.types.VARCHAR() - return sa.types.VARCHAR() diff --git a/tests/core/test_typing.py b/tests/core/test_typing.py index 25d9c68df..219c9a587 100644 --- a/tests/core/test_typing.py +++ b/tests/core/test_typing.py @@ -345,6 +345,10 @@ def test_conform_primitives(): sa.types.DATETIME, ), ({"anyOf": [{"type": "integer"}, {"type": "null"}]}, sa.types.INTEGER), + ( + {"type": ["array", "object", "boolean", "null"]}, + sa.types.VARCHAR, + ), ], ) def test_to_sql_type(jsonschema_type, expected):