Skip to content

Commit 1eeacb2

Browse files
authored
Fix element type inference in YAML::ArrayConverter.from_yaml (#16166)
1 parent 9713229 commit 1eeacb2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

spec/std/yaml/serializable_spec.cr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,23 @@ class YAMLAttrWithTimeArray3
148148
property value : Array(Time)
149149
end
150150

151+
module YAMLAttrPointConverter
152+
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
153+
YAMLAttrPoint.new(ctx, node)
154+
end
155+
156+
def self.to_yaml(value, yaml : YAML::Nodes::Builder)
157+
value.to_yaml(yaml)
158+
end
159+
end
160+
161+
class YAMLAttrWithSerializableArray
162+
include YAML::Serializable
163+
164+
@[YAML::Field(converter: YAML::ArrayConverter(YAMLAttrPointConverter))]
165+
property value : Array(YAMLAttrPoint)
166+
end
167+
151168
class YAMLAttrWithSimpleMapping
152169
include YAML::Serializable
153170

@@ -986,6 +1003,14 @@ describe "YAML::Serializable" do
9861003
yaml.value.map(&.to_s).should eq(["2014-10-31 23:37:16 UTC"])
9871004
yaml.to_yaml.should eq(string)
9881005
end
1006+
1007+
it "uses correct array element type" do
1008+
string = %(---\nvalue:\n- x: 1\n y: 2\n)
1009+
yaml = YAMLAttrWithSerializableArray.from_yaml(string)
1010+
yaml.value.should be_a(Array(YAMLAttrPoint))
1011+
yaml.value.should eq([YAMLAttrPoint.new(1, 2)])
1012+
yaml.to_yaml.should eq(string)
1013+
end
9891014
end
9901015

9911016
it "parses nilable union" do

src/yaml/from_yaml.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,10 @@ module YAML::ArrayConverter(Converter)
376376
node.raise "Expected sequence, not #{node.kind}"
377377
end
378378

379-
ary = Array(typeof(@converter.from_yaml(ctx, node))).new
379+
ary = Array(typeof(begin
380+
value = uninitialized YAML::Nodes::Node
381+
@converter.from_yaml(ctx, value)
382+
end)).new
380383

381384
node.each do |value|
382385
ary << @converter.from_yaml(ctx, value)

0 commit comments

Comments
 (0)