diff --git a/spec/std/yaml/serializable_spec.cr b/spec/std/yaml/serializable_spec.cr index 701c2a939ccf..968326d2963a 100644 --- a/spec/std/yaml/serializable_spec.cr +++ b/spec/std/yaml/serializable_spec.cr @@ -304,6 +304,12 @@ class YAMLCircle < YAMLShape property radius : Int32 end +class YAMLWithShape + include YAML::Serializable + + property shape : YAMLShape +end + describe "YAML::Serializable" do it "works with record" do YAMLAttrPoint.new(1, 2).to_yaml.should eq "---\nx: 1\ny: 2\n" @@ -831,5 +837,12 @@ describe "YAML::Serializable" do YAMLShape.from_yaml(%({"type": "unknown"})) end end + + it "deserializes type which nests type with discriminator (#9849)" do + container = YAMLWithShape.from_yaml(%({"shape": {"type": "point", "x": 1, "y": 2}})) + point = container.shape.as(YAMLPoint) + point.x.should eq(1) + point.y.should eq(2) + end end end diff --git a/src/yaml/serialization.cr b/src/yaml/serialization.cr index 10b0ae2b8f1e..d2d3a9603064 100644 --- a/src/yaml/serialization.cr +++ b/src/yaml/serialization.cr @@ -381,7 +381,7 @@ module YAML {% mapping.raise "mapping argument must be a HashLiteral or a NamedTupleLiteral, not #{mapping.class_name.id}" %} {% end %} - private def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) + def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) ctx.read_alias(node, \{{@type}}) do |obj| return obj end