Repository: meltano/sdk
Pull request #2683 (2024-09-23 17:41:50)

fix: A clear error message is now emitted when flattening is enable but `flattening_max_depth` is not set
See https://github.com/MeltanoLabs/tap-stackexchange/issues/41

<!-- readthedocs-preview meltano-sdk start -->
----
📚 Documentation preview 📚: https://meltano-sdk--2683.org.readthedocs.build/en/2683/

<!-- readthedocs-preview meltano-sdk end -->

codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/meltano/sdk/branches/edgarrmondragon/fix/clear-error-message-missing-flattening-max-depth)

### Merging #2683 will **not alter performance**

<sub>Comparing <code>edgarrmondragon/fix/clear-error-message-missing-flattening-max-depth</code> (8a609f6) with <code>main</code> (3d3ac7b)</sub>



### Summary

`✅ 6` untouched benchmarks





codecov[bot]: ## [Codecov](https://app.codecov.io/gh/meltano/sdk/pull/2683?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 90.25%. Comparing base [(`3d3ac7b`)](https://app.codecov.io/gh/meltano/sdk/commit/3d3ac7b816297b75430b89c0f667a60afea74d56?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano) to head [(`8eb341d`)](https://app.codecov.io/gh/meltano/sdk/commit/8eb341d8e38899ec247106ad772fd903cb61efe8?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano).
> Report is 1 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #2683   +/-   ##
=======================================
  Coverage   90.24%   90.25%           
=======================================
  Files          58       58           
  Lines        4891     4895    +4     
  Branches      963      964    +1     
=======================================
+ Hits         4414     4418    +4     
  Misses        329      329           
  Partials      148      148           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/meltano/sdk/pull/2683?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=meltano).


Patch:
diff --git a/singer_sdk/helpers/_flattening.py b/singer_sdk/helpers/_flattening.py
index 79ca50fd..883d8a1a 100644
--- a/singer_sdk/helpers/_flattening.py
+++ b/singer_sdk/helpers/_flattening.py
@@ -11,6 +11,7 @@ from copy import deepcopy
 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")) 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
 


Test patch:
diff --git a/tests/core/test_flattening.py b/tests/core/test_flattening.py
index 1e046698..f56abc8b 100644
--- a/tests/core/test_flattening.py
+++ b/tests/core/test_flattening.py
@@ -2,7 +2,8 @@ from __future__ import annotations
 
 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"
+    )