Skip to content

Commit bfad07b

Browse files
authored
Fix: Handle array types with json schema parsing errors in subtypes (#113)
1 parent 5cbde18 commit bfad07b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

airbyte/types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ def _get_airbyte_type( # noqa: PLR0911 # Too many return statements
7171
if json_schema_type == "array":
7272
items_def = json_schema_property_def.get("items", None)
7373
if isinstance(items_def, dict):
74-
subtype, _ = _get_airbyte_type(items_def)
74+
try:
75+
subtype, _ = _get_airbyte_type(items_def)
76+
except SQLTypeConversionError:
77+
# We have enough information, so we can ignore parsing errors on subtype.
78+
subtype = None
79+
7580
return "array", subtype
7681

7782
return "array", None

tests/unit_tests/test_type_translation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
({"type": "number", "airbyte_type": "integer"}, types.BIGINT),
2828
({"type": "number"}, types.DECIMAL),
2929
({"type": "array", "items": {"type": "object"}}, types.JSON),
30+
({"type": ["null", "array"], "items": {"type": "object"}}, types.JSON),
3031
({"type": "object", "properties": {}}, types.JSON),
32+
({"type": ["null", "object"], "properties": {}}, types.JSON),
33+
# Malformed JSON schema seen in the wild:
34+
({'type': 'array', 'items': {}}, types.JSON),
35+
({'type': ['null', 'array'], 'items': {'items': {}}}, types.JSON),
3136
],
3237
)
3338
def test_to_sql_type(json_schema_property_def, expected_sql_type):
@@ -53,8 +58,15 @@ def test_to_sql_type(json_schema_property_def, expected_sql_type):
5358
({"type": "integer"}, "integer"),
5459
({"type": "number", "airbyte_type": "integer"}, "integer"),
5560
({"type": "number"}, "number"),
61+
# Array type:
5662
({"type": "array"}, "array"),
63+
({"type": "array", "items": {"type": "object"}}, "array"),
64+
({"type": ["null", "array"], "items": {"type": "object"}}, "array"),
65+
# Object type:
5766
({"type": "object"}, "object"),
67+
# Malformed JSON schema seen in the wild:
68+
({'type': 'array', 'items': {'items': {}}}, "array"),
69+
({'type': ['null', 'array'], 'items': {'items': {}}}, "array"),
5870
],
5971
)
6072
def test_to_airbyte_type(json_schema_property_def, expected_airbyte_type):
@@ -71,6 +83,9 @@ def test_to_airbyte_type(json_schema_property_def, expected_airbyte_type):
7183
({"type": "object"}, "object", None),
7284
({"type": "array", "items": {"type": ["null", "string"]}}, "array", "string"),
7385
({"type": "array", "items": {"type": ["boolean"]}}, "array", "boolean"),
86+
# Malformed JSON schema seen in the wild:
87+
({'type': 'array', 'items': {'items': {}}}, "array", None),
88+
({'type': ['null', 'array'], 'items': {'items': {}}}, "array", None),
7489
],
7590
)
7691
def test_to_airbyte_subtype(

0 commit comments

Comments
 (0)