Skip to content

Commit b535700

Browse files
committed
Raise exception on duplicate keys
1 parent 3482ad1 commit b535700

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/psych/handlers/document_stream.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def end_document implicit_end = !streaming?
1818
@last.implicit_end = implicit_end
1919
@block.call pop
2020
end
21+
22+
def end_mapping
23+
mapping = pop
24+
keys = {}
25+
mapping.children.each_slice(2) do |(key_scalar, _)|
26+
next if key_scalar.is_a?(Psych::Nodes::Sequence) or key_scalar.is_a?(Psych::Nodes::Alias) or key_scalar.is_a?(Psych::Nodes::Mapping)
27+
key = key_scalar.value
28+
raise Psych::Exception, "Duplicate key #{key} exists on this level" if keys.key? key
29+
keys[key] = nil
30+
end
31+
mapping
32+
end
2133
end
2234
end
2335
end

0 commit comments

Comments
 (0)