Skip to content

Commit

Permalink
fix: A clear error message is now emitted when flattening is enable b…
Browse files Browse the repository at this point in the history
…ut `flattening_max_depth` is not set (#2683)
  • Loading branch information
edgarrmondragon committed Sep 23, 2024
1 parent 765a378 commit e997deb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion singer_sdk/helpers/_flattening.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import inflection

from singer_sdk._singerlib.json import serialize_json
from singer_sdk.exceptions import ConfigValidationError

DEFAULT_FLATTENING_SEPARATOR = "__"

Expand All @@ -35,7 +36,14 @@ def get_flattening_options(
A new FlatteningOptions object or None if flattening is disabled.
"""
if plugin_config.get("flattening_enabled", False):
return FlatteningOptions(max_level=int(plugin_config["flattening_max_depth"]))
if (max_depth := plugin_config.get("flattening_max_depth")) is not None:
return FlatteningOptions(max_level=int(max_depth))

msg = "Flattening is misconfigured"
raise ConfigValidationError(
msg,
errors=["flattening_max_depth is required when flattening is enabled"],
)

return None

Expand Down
15 changes: 14 additions & 1 deletion tests/core/test_flattening.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

from singer_sdk.helpers._flattening import flatten_record
from singer_sdk.exceptions import ConfigValidationError
from singer_sdk.helpers._flattening import flatten_record, get_flattening_options


@pytest.mark.parametrize(
Expand Down Expand Up @@ -72,3 +73,15 @@ def test_flatten_record(flattened_schema, max_level, expected):
record, max_level=max_level, flattened_schema=flattened_schema
)
assert expected == result


def test_get_flattening_options_missing_max_depth():
with pytest.raises(
ConfigValidationError, match="Flattening is misconfigured"
) as exc:
get_flattening_options({"flattening_enabled": True})

assert (
exc.value.errors[0]
== "flattening_max_depth is required when flattening is enabled"
)

0 comments on commit e997deb

Please sign in to comment.