ruby-changes:67502
From: Aaron <ko1@a...>
Date: Tue, 31 Aug 2021 19:36:37 +0900 (JST)
Subject: [ruby-changes:67502] 9ed2cb26de (master): [ruby/psych] Add quotes to the strings "y" and "n"
https://git.ruby-lang.org/ruby.git/commit/?id=9ed2cb26de From 9ed2cb26dee8ed801a75cf4b276f1ec354ade032 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <tenderlove@r...> Date: Wed, 4 Aug 2021 09:27:04 -0700 Subject: [ruby/psych] Add quotes to the strings "y" and "n" 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in YAML documents as strings. But this is not what the YAML 1.1 spec says. YAML 1.1 says they should be treated as booleans. When we're dumping documents, we know it's a string, so adding quotes will eliminate the "ambiguity" in the emitted document Fixes #443 https://github.com/ruby/psych/commit/6a1c30634e --- ext/psych/lib/psych/visitors/yaml_tree.rb | 2 ++ test/psych/test_string.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index 05748dd..2eee4d3 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -272,6 +272,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L272 tag = 'tag:yaml.org,2002:str' plain = false quote = false + elsif o == 'y' || o == 'n' + style = Nodes::Scalar::DOUBLE_QUOTED elsif @line_width && o.length > @line_width style = Nodes::Scalar::FOLDED elsif o =~ /^[^[:word:]][^"]*$/ diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 20ab79c..0dc34b3 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -17,6 +17,19 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_string.rb#L17 end end + # 'y' and 'n' are kind of ambiguous. Syck treated y and n literals in + # YAML documents as strings. But this is not what the YAML 1.1 spec says. + # YAML 1.1 says they should be treated as booleans. When we're dumping + # documents, we know it's a string, so adding quotes will eliminate the + # "ambiguity" in the emitted document + def test_y_is_quoted + assert_match(/"y"/, Psych.dump("y")) + end + + def test_n_is_quoted + assert_match(/"n"/, Psych.dump("n")) + end + def test_string_with_newline assert_equal "1\n2", Psych.load("--- ! '1\n\n 2'\n") end -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/