Skip to content

Commit

Permalink
[ruby/psych] Add quotes to the strings "y" and "n"
Browse files Browse the repository at this point in the history
'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

ruby/psych@6a1c30634e
  • Loading branch information
tenderlove authored and hsbt committed Aug 31, 2021
1 parent 0925fdd commit 9ed2cb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ext/psych/lib/psych/visitors/yaml_tree.rb
Expand Up @@ -272,6 +272,8 @@ def visit_String o
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:]][^"]*$/
Expand Down
13 changes: 13 additions & 0 deletions test/psych/test_string.rb
Expand Up @@ -17,6 +17,19 @@ def initialize
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
Expand Down

0 comments on commit 9ed2cb2

Please sign in to comment.