diff --git a/src/python/jw/util/load_yaml.py b/src/python/jw/util/load_yaml.py index 6f9d001..35633e5 100644 --- a/src/python/jw/util/load_yaml.py +++ b/src/python/jw/util/load_yaml.py @@ -225,10 +225,15 @@ def _expand_links_recursive( value, root, current_path + (key, ), resolved, resolving ) + if not expanded_remaining: + return base_value + if not isinstance(base_value, dict): + raise ValueError( + f'jw.inplace-link {link_spec!r}: resolved value is not ' + f'a mapping, cannot merge siblings' + ) # Deep-merge siblings into resolved dict (siblings win on conflict) - if isinstance(base_value, dict) and expanded_remaining: - return _deep_merge(base_value, expanded_remaining) - return base_value + return _deep_merge(base_value, expanded_remaining) # --- jw.link (sole key): inplace or named replacement --- if 'jw.link' in node and len(node) == 1: diff --git a/test/unit/python/jw/util/load_yaml/test.py b/test/unit/python/jw/util/load_yaml/test.py index b933114..db3b238 100644 --- a/test/unit/python/jw/util/load_yaml/test.py +++ b/test/unit/python/jw/util/load_yaml/test.py @@ -669,10 +669,30 @@ class TestInplaceLink: assert data['entry']['flag'] is True def test_inplace_link_non_dict_target(self, yaml_file: Callable) -> None: - """Non-dict target replaces entirely; siblings dropped.""" + """Non-dict target without siblings replaces the value.""" data = load_yaml(yaml_file('target: val\nentry:\n jw.inplace-link: target\n')) assert data['entry'] == 'val' + def test_inplace_link_non_dict_target_siblings_raise( + self, yaml_file: Callable + ) -> None: + """Non-dict target with sibling keys raises instead of dropping them.""" + with pytest.raises(ValueError, match = 'not a mapping'): + load_yaml( + yaml_file( + 'target: val\nentry:\n jw.inplace-link: target\n extra: kept\n' + ) + ) + + def test_inplace_link_list_target_siblings_raise(self, yaml_file: Callable) -> None: + """List target with sibling keys raises instead of dropping them.""" + with pytest.raises(ValueError, match = 'not a mapping'): + load_yaml( + yaml_file( + 'target:\n - a\nentry:\n jw.inplace-link: target\n extra: kept\n' + ) + ) + def test_inplace_link_relative_path(self, yaml_file: Callable) -> None: """jw.inplace-link supports relative paths.""" data = load_yaml(