Skip to content

Commit

Permalink
YAML: allow using disciminator in contained types (crystal-lang#9851)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 26, 2020
1 parent 0cfcead commit 4dca19f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions spec/std/yaml/serializable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/yaml/serialization.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4dca19f

Please sign in to comment.