Skip to content

Commit c1d0064

Browse files
committed
mref with hash merging
1 parent a8bdc10 commit c1d0064

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/test/test_yaml_loader.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@
77

88
class TestYamlLoader < Minitest::Test
99

10+
def test_that_mref_with_nested_replace_works
11+
yaml = <<~YAML
12+
base:
13+
key1:
14+
sub_key1: value1
15+
key2: value2
16+
17+
middle:
18+
$mref: "#/base"
19+
key3: value3
20+
key4: value4
21+
22+
bottom:
23+
$mref:
24+
- "#/base"
25+
- "#/middle"
26+
key1:
27+
sub_key6: value6
28+
key2: value2_new
29+
key4: value4_new
30+
key5: value5
31+
YAML
32+
33+
f = Tempfile.new("yml")
34+
f.write(yaml)
35+
f.flush
36+
37+
doc = YamlLoader.load(f.path)
38+
assert_equal({ "key1" => {"sub_key1" => "value1", "sub_key6" => "value6"}, "key2" => "value2_new", "key3" => "value3", "key4" => "value4_new", "key5" => "value5" }, doc["bottom"])
39+
end
40+
1041
def test_that_spurious_recursive_mref_works
1142
yaml = <<~YAML
1243
base:

lib/yaml_loader.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,13 @@ def self.expand(filename, obj, yaml_opts = {})
8383
obj_keys.each do |key|
8484
value = obj[key]
8585

86-
new_obj[key] = expand(filename, value, yaml_opts)
86+
expanded = expand(filename, value, yaml_opts)
87+
if new_obj[key].is_a?(Hash)
88+
raise "should be a hash" unless expanded.is_a?(Hash)
89+
new_obj[key].merge!(expanded)
90+
else
91+
new_obj[key] = expanded
92+
end
8793
end
8894
new_obj
8995
else

0 commit comments

Comments
 (0)