Repository: meltano/sdk
Pull request #2727 (2024-10-18 23:53:25)

fix: Object and array JSON types are now handled before primitive types when converting them to SQL types
- **Add failing test**
- **Prioritize object and array**


<!-- readthedocs-preview meltano-sdk start -->
----
📚 Documentation preview 📚: https://meltano-sdk--2727.org.readthedocs.build/en/2727/

<!-- readthedocs-preview meltano-sdk end -->

codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/meltano/sdk/branches/2726-bug-json-schema-union-types-that-contain-boolean-result-in-casting-json-objects-and-arrays-as-boolean-values)

### Merging #2727 will **not alter performance**

<sub>Comparing <code>2726-bug-json-schema-union-types-that-contain-boolean-result-in-casting-json-objects-and-arrays-as-boolean-values</code> (1aa3b3c) with <code>main</code> (316282e)</sub>



### Summary

`✅ 6` untouched benchmarks





codecov[bot]: ## [Codecov](https://app.codecov.io/gh/meltano/sdk/pull/2727?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 90.70%. Comparing base [(`316282e`)](https://app.codecov.io/gh/meltano/sdk/commit/316282e08d04e17d0917e344a8bcdc012e30042b?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) to head [(`1aa3b3c`)](https://app.codecov.io/gh/meltano/sdk/commit/1aa3b3c7061fa42d0bd8441bf78434a62907c9f9?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #2727   +/-   ##
=======================================
  Coverage   90.70%   90.70%           
=======================================
  Files          62       62           
  Lines        5016     5016           
  Branches      641      641           
=======================================
  Hits         4550     4550           
  Misses        325      325           
  Partials      141      141           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/meltano/sdk/pull/2727?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano).


Patch:
diff --git a/singer_sdk/typing.py b/singer_sdk/typing.py
index 845f1dcd..a21447a4 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()


Test patch:
diff --git a/tests/core/test_typing.py b/tests/core/test_typing.py
index 25d9c68d..219c9a58 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):