From 8a609f61dafdaf2fcc073177a26c38f80bbc1cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez-Mondrag=C3=B3n?= Date: Mon, 23 Sep 2024 11:41:28 -0600 Subject: [PATCH] fix: A clear error message is now emitted when flattening is enable but `flattening_max_depth` is not set --- singer_sdk/helpers/_flattening.py | 10 +++++++++- tests/core/test_flattening.py | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/singer_sdk/helpers/_flattening.py b/singer_sdk/helpers/_flattening.py index 79ca50fdc..dee8c842d 100644 --- a/singer_sdk/helpers/_flattening.py +++ b/singer_sdk/helpers/_flattening.py @@ -11,6 +11,7 @@ import inflection from singer_sdk._singerlib.json import serialize_json +from singer_sdk.exceptions import ConfigValidationError DEFAULT_FLATTENING_SEPARATOR = "__" @@ -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"): + 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 diff --git a/tests/core/test_flattening.py b/tests/core/test_flattening.py index 1e0466986..f56abc8be 100644 --- a/tests/core/test_flattening.py +++ b/tests/core/test_flattening.py @@ -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( @@ -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" + )