Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Handle anyOf types other than string, array, object #229

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions target_snowflake/flattening.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def flatten_schema(d, parent_key=None, sep='__', level=0, max_level=0):
else:
items.append((new_key, v))
else:
# handle situations where, e.g., an `anyOf` value is passed
if len(v.values()) > 0:
if list(v.values())[0][0]['type'] == 'string':
list(v.values())[0][0]['type'] = ['null', 'string']
Expand All @@ -63,6 +64,9 @@ def flatten_schema(d, parent_key=None, sep='__', level=0, max_level=0):
elif list(v.values())[0][0]['type'] == 'object':
list(v.values())[0][0]['type'] = ['null', 'object']
items.append((new_key, list(v.values())[0][0]))
else:
list(v.values())[0][0]['type'] = ['null', 'string']
items.append((new_key, list(v.values())[0][0]))

key_func = lambda item: item[0]
sorted_items = sorted(items, key=key_func)
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_flattening.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ def test_flatten_schema(self):
}
}

not_nested_schema_with_anyof_property_type = {
"type": "object",
"properties": {
"object_col": {"anyOf": [{"type": "object"}, {"type": ["null", "string"]}]},
"array_col": {"anyOf": [{"type": "array"}, {"type": ["null", "string"]}]},
"bool_col": {"anyOf": [{"type": ["boolean", "null"]}, {"type": ["null", "string"]}]}
}
}
flattened_schema_with_anyof_property_type = {
"object_col": {"type": ["null", "object"]},
"array_col": {"type": ["null", "array"]},
"bool_col": {"type": ["null", "string"]}
}

# NO FLATTENING - Schema with anyOf properties should be cast to a single data type
self.assertEqual(flatten_schema(not_nested_schema_with_anyof_property_type),
flattened_schema_with_anyof_property_type)

# NO FLATTENING - Schema with object type property but without further properties should be a plain dictionary
# No flattening (default)
self.assertEqual(flatten_schema(nested_schema_with_properties), nested_schema_with_properties['properties'])
Expand Down