Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: Force flattened record according to provided flattened schema #2243

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
5 changes: 4 additions & 1 deletion singer_sdk/helpers/_flattening.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ def _flatten_record(
items: list[tuple[str, t.Any]] = []
for k, v in record_node.items():
new_key = flatten_key(k, parent_key, separator)
if isinstance(v, collections.abc.MutableMapping) and level < max_level:
if isinstance(v, collections.abc.MutableMapping) and (
(new_key not in flattened_schema.get("properties", {}))
and (level < max_level)
):
items.extend(
_flatten_record(
v,
Expand Down
74 changes: 74 additions & 0 deletions tests/core/test_flattening.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from __future__ import annotations

import pytest

from singer_sdk.helpers._flattening import flatten_record


@pytest.mark.parametrize(
"flattened_schema, max_level, expected",
[
pytest.param(
{
"properties": {
"key_1": {"type": ["null", "integer"]},
"key_2__key_3": {"type": ["null", "string"]},
"key_2__key_4": {"type": ["null", "object"]},
}
},
99,
{
"key_1": 1,
"key_2__key_3": "value",
"key_2__key_4": '{"key_5": 1, "key_6": ["a", "b"]}',
},
id="flattened schema limiting the max level",
),
pytest.param(
{
"properties": {
"key_1": {"type": ["null", "integer"]},
"key_2__key_3": {"type": ["null", "string"]},
"key_2__key_4__key_5": {"type": ["null", "integer"]},
"key_2__key_4__key_6": {"type": ["null", "array"]},
}
},
99,
{
"key_1": 1,
"key_2__key_3": "value",
"key_2__key_4__key_5": 1,
"key_2__key_4__key_6": '["a", "b"]',
},
id="flattened schema not limiting the max level",
),
pytest.param(
{
"properties": {
"key_1": {"type": ["null", "integer"]},
"key_2__key_3": {"type": ["null", "string"]},
"key_2__key_4__key_5": {"type": ["null", "integer"]},
"key_2__key_4__key_6": {"type": ["null", "array"]},
}
},
1,
{
"key_1": 1,
"key_2__key_3": "value",
"key_2__key_4": '{"key_5": 1, "key_6": ["a", "b"]}',
},
id="max level limiting flattened schema",
),
edgarrmondragon marked this conversation as resolved.
Show resolved Hide resolved
],
)
def test_flatten_record(flattened_schema, max_level, expected):
"""Test flatten_record to obey the max_level and flattened_schema parameters."""
record = {
"key_1": 1,
"key_2": {"key_3": "value", "key_4": {"key_5": 1, "key_6": ["a", "b"]}},
}

result = flatten_record(
record, max_level=max_level, flattened_schema=flattened_schema
)
assert expected == result
Loading