From 31f0c593d7f131355fb3891d2df099bf1b7013bb Mon Sep 17 00:00:00 2001 From: Will Richardson Date: Sat, 3 Jun 2023 14:21:18 +0000 Subject: [PATCH] Fix handling of quoted boolean values in `YAML::Any` `"yes"` and `"no"` should be treated as strings (since they're quoted) whereas `yes` and `no` should be booleans (since they're plain). This is already handled in most places, but `YAML::Any` passes the node value, rather than the node itself when handling a scalar and so the quoting type is lost, and everything is parsed as though it is unquoted. --- spec/std/yaml/any_spec.cr | 20 ++++++++++++++++++++ src/yaml/any.cr | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/spec/std/yaml/any_spec.cr b/spec/std/yaml/any_spec.cr index 33c8dc458db4..ffab8f14babc 100644 --- a/spec/std/yaml/any_spec.cr +++ b/spec/std/yaml/any_spec.cr @@ -236,6 +236,26 @@ describe YAML::Any do YAML.parse("*foo") end end + + it "gets yes/no unquoted booleans" do + YAML.parse("yes").as_bool.should be_true + YAML.parse("no").as_bool.should be_false + YAML.parse("'yes'").as_bool?.should be_nil + YAML.parse("'no'").as_bool?.should be_nil + YAML::Any.from_yaml("yes").as_bool.should be_true + YAML::Any.from_yaml("no").as_bool.should be_false + YAML::Any.from_yaml("'yes'").as_bool?.should be_nil + YAML::Any.from_yaml("'no'").as_bool?.should be_nil + end + + it "doesn't get quoted numbers" do + YAML.parse("1").as_i64.should eq(1) + YAML.parse("'1'").as_i64?.should be_nil + YAML.parse("'1'").as_s?.should eq("1") + YAML::Any.from_yaml("1").as_i64.should eq(1) + YAML::Any.from_yaml("'1'").as_i64?.should be_nil + YAML::Any.from_yaml("'1'").as_s?.should eq("1") + end end describe "#size" do diff --git a/src/yaml/any.cr b/src/yaml/any.cr index 8acd9fa057df..1518623efd5f 100644 --- a/src/yaml/any.cr +++ b/src/yaml/any.cr @@ -31,7 +31,7 @@ struct YAML::Any def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) case node when YAML::Nodes::Scalar - new YAML::Schema::Core.parse_scalar(node.value) + new YAML::Schema::Core.parse_scalar(node) when YAML::Nodes::Sequence ary = [] of YAML::Any