Skip to content

Commit

Permalink
Fix symabolize_name with non-string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Feb 25, 2021
1 parent 091cd46 commit 1c5c29e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/psych/visitors/to_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def revive_hash hash, o, tagged= false
hash[key] = val
end
else
if !tagged && @symbolize_names
if !tagged && @symbolize_names && key.is_a?(String)
key = key.to_sym
elsif !@freeze
key = deduplicate(key)
Expand Down
7 changes: 4 additions & 3 deletions test/psych/test_psych.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,17 +371,18 @@ def test_symbolize_names
yaml = <<-eoyml
foo:
bar: baz
1: 2
hoge:
- fuga: piyo
eoyml

result = Psych.load(yaml)
assert_equal result, { "foo" => { "bar" => "baz"}, "hoge" => [{ "fuga" => "piyo" }] }
assert_equal result, { "foo" => { "bar" => "baz", 1 => 2 }, "hoge" => [{ "fuga" => "piyo" }] }

result = Psych.load(yaml, symbolize_names: true)
assert_equal result, { foo: { bar: "baz" }, hoge: [{ fuga: "piyo" }] }
assert_equal result, { foo: { bar: "baz", 1 => 2 }, hoge: [{ fuga: "piyo" }] }

result = Psych.safe_load(yaml, symbolize_names: true)
assert_equal result, { foo: { bar: "baz" }, hoge: [{ fuga: "piyo" }] }
assert_equal result, { foo: { bar: "baz", 1 => 2 }, hoge: [{ fuga: "piyo" }] }
end
end

0 comments on commit 1c5c29e

Please sign in to comment.