diff --git a/lib/psych/handlers/document_stream.rb b/lib/psych/handlers/document_stream.rb index b77115d0..bf555ebf 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 diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 53747813..72ef06a6 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -92,6 +92,16 @@ def test_map assert_equal X, x.class end + def test_error_on_same_key + assert_raises(Psych::Exception) do + Psych.load <<-EOF + - + same_key: 'value' + same_key: 'value' + EOF + end + end + def test_self_referential @hash['self'] = @hash assert_cycle(@hash)