Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/test/test_yaml_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,50 @@

class TestYamlLoader < Minitest::Test


def test_remove
yaml = <<~YAML
base:
key1: value1
key2: value2

child:
$mref: "#/base"
$remove: key2
key3: value3
YAML

f = Tempfile.new("yml")
f.write(yaml)
f.flush

doc = YamlLoader.load(f.path)
assert_equal({ "key1" => "value1", "key3" => "value3" }, doc["child"])
end

def test_multiple_remove
yaml = <<~YAML
base:
key1: value1
key2: value2
key3: value3

child:
$mref: "#/base"
$remove:
- key2
- key3
key4: value4
YAML

f = Tempfile.new("yml")
f.write(yaml)
f.flush

doc = YamlLoader.load(f.path)
assert_equal({ "key1" => "value1", "key4" => "value4" }, doc["child"])
end

def test_that_mref_with_nested_replace_works
yaml = <<~YAML
base:
Expand Down
13 changes: 13 additions & 0 deletions lib/yaml_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def self.expand(filename, obj, yaml_opts = {})

return obj.map { |v| expand(filename, v, yaml_opts) } if obj.is_a?(Array)

new_obj =
if obj.keys.include?("$ref")
# according JSON Reference, all keys except $ref are ignored
relative_path = obj["$ref"].split("#")[0]
Expand Down Expand Up @@ -103,7 +104,9 @@ def self.expand(filename, obj, yaml_opts = {})
end
end
end

final_obj

else
obj_keys = obj.keys
obj_keys.each do |key|
Expand All @@ -113,6 +116,16 @@ def self.expand(filename, obj, yaml_opts = {})
end
obj
end

obj_keys = new_obj.keys
if obj_keys.include? "$remove"
remove_keys = obj["$remove"].is_a?(Array) ? obj["$remove"] : [obj["$remove"]]
remove_keys.each do |key|
new_obj.delete(key)
end
end
new_obj.delete("$remove")
new_obj
end

# load a YAML file and expand any $ref/$mref references
Expand Down
Loading