From 17bb7d112e72577c4980a9335c8f0bc825093095 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 23 Nov 2026 20:19:59 +0900 Subject: [PATCH] Do not treat a quoted << key as a merge key A quoted scalar is a string, so '<<' and "<<" should be ordinary keys, as with !!str '<<'. Only the !!str case was handled for #203. Co-Authored-By: Claude Opus 5.5 --- lib/psych/visitors/to_ruby.rb | 2 +- test/psych/test_merge_keys.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index d0bf0707..79bcc8ed 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -390,7 +390,7 @@ def revive_hash hash, o, tagged= false key = accept(k) val = accept(v) - if key == '<<' && k.tag != "tag:yaml.org,2002:str" + if key == '<<' && k.tag != "tag:yaml.org,2002:str" && !k.quoted case v when Nodes::Alias, Nodes::Mapping begin diff --git a/test/psych/test_merge_keys.rb b/test/psych/test_merge_keys.rb index 2f55a1ed..d1e5fc20 100644 --- a/test/psych/test_merge_keys.rb +++ b/test/psych/test_merge_keys.rb @@ -47,6 +47,13 @@ def test_explicit_string assert_equal expected, doc end + def test_quoted_string + expected = { "a" => "x", "<<" => { "a" => "y" } } + assert_equal expected, Psych.load("a: x\n'<<': { a: y }\n") + assert_equal expected, Psych.load("a: x\n\"<<\": { a: y }\n") + assert_equal expected, Psych.load('{"a": "x", "<<": {"a": "y"}}') + end + def test_mergekey_with_object s = <<-eoyml foo: &foo