From b5357005d0fe9eebdcd008aa6967027124b7a00c Mon Sep 17 00:00:00 2001 From: Aaron Brager Date: Tue, 17 Dec 2019 17:34:59 -0800 Subject: [PATCH] Raise exception on duplicate keys --- lib/psych/handlers/document_stream.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/psych/handlers/document_stream.rb b/lib/psych/handlers/document_stream.rb index 67da7940..cd5cac1d 100644 --- a/lib/psych/handlers/document_stream.rb +++ b/lib/psych/handlers/document_stream.rb @@ -18,6 +18,18 @@ def end_document implicit_end = !streaming? @last.implicit_end = implicit_end @block.call pop end + + def end_mapping + mapping = pop + keys = {} + mapping.children.each_slice(2) do |(key_scalar, _)| + 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) + key = key_scalar.value + raise Psych::Exception, "Duplicate key #{key} exists on this level" if keys.key? key + keys[key] = nil + end + mapping + end end end end