Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDK: improve TypeTransformer to convert simple types to array of simple types #16636

Merged
merged 11 commits into from
Sep 15, 2022
1 change: 1 addition & 0 deletions airbyte-cdk/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## 0.1.86
- TypeTransformer now converts simple types to array of simple types
- TypeTransformer make warning message more informative

## 0.1.85
Expand Down
7 changes: 6 additions & 1 deletion airbyte-cdk/python/airbyte_cdk/sources/utils/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from jsonschema import Draft7Validator, ValidationError, validators

json_to_python = {"string": str, "number": float, "integer": int, "boolean": bool, "null": type(None), "object": dict, "array": list}
json_to_python_simple = {"string": str, "number": float, "integer": int, "boolean": bool, "null": type(None)}
json_to_python = json_to_python_simple | {"object": dict, "array": list}
python_to_json = {v: k for k, v in json_to_python.items()}

logger = logging.getLogger("airbyte")
Expand Down Expand Up @@ -117,6 +118,10 @@ def default_convert(original_item: Any, subschema: Dict[str, Any]) -> Any:
if isinstance(original_item, str):
return strtobool(original_item) == 1
return bool(original_item)
elif target_type == "array":
item_types = set(subschema.get("items", {}).get("type", set()))
if item_types.issubset(json_to_python_simple) and type(original_item) in json_to_python_simple.values():
return [original_item]
except (ValueError, TypeError):
return original_item
return original_item
Expand Down
40 changes: 38 additions & 2 deletions airbyte-cdk/python/unit_tests/sources/utils/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,44 @@
# Array without items and value is not an array
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": "12"},
{"value": "12"},
"Failed to transform value '12' of type 'string' to 'array', key path: '.value'",
{"value": ["12"]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": 12},
{"value": [12]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array"}}},
{"value": None},
{"value": [None]},
None,
),
(
{"type": "object", "properties": {"value": {"type": ["null", "array"]}}},
{"value": None},
{"value": None},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array", "items": {"type": ["string"]}}}},
{"value": 10},
{"value": ["10"]},
None,
),
(
{"type": "object", "properties": {"value": {"type": "array", "items": {"type": ["object"]}}}},
{"value": "string"},
{"value": "string"},
"Failed to transform value 'string' of type 'string' to 'array', key path: '.value'",
),
(
{"type": "object", "properties": {"value": {"type": "array", "items": {"type": ["string"]}}}},
{"value": {"key": "value"}},
{"value": {"key": "value"}},
"Failed to transform value {'key': 'value'} of type 'object' to 'array', key path: '.value'",
),
(
# Schema root object is not an object, no convertion should happen
Expand Down