Skip to content

from_yaml does not support value type anchors #13830

@HertzDevil

Description

@HertzDevil

YAML::ParseContext#record_anchor, which is used by the various from_yaml methods and YAML::Serializable, does nothing to an object unless it has a reference type. That means an alias will fail to resolve if its target has a value type, even though YAML.parse will succeed:

require "yaml"

record Foo, x : Int32, y : Int32 do
  include YAML::Serializable
end

yaml = <<-YAML
x: &anchor 123
y: *anchor
YAML

YAML.parse(yaml)    # => {"x" => 123, "y" => 123}
Foo.from_yaml(yaml) # Unhandled exception: Error deserializing alias (Exception)

Hash(String, Int32).from_yaml(yaml) # Unhandled exception: Error deserializing alias (Exception)

The exception type is not even YAML::ParseException, just Exception.

Reference type anchors are okay:

record Bar, x : String, y : String do
  include YAML::Serializable
end

yaml = <<-YAML
x: &anchor "abc"
y: *anchor
YAML

bar = Bar.from_yaml(yaml) # => Bar(@x="abc", @y="abc")
bar.x.same?(bar.y)        # => true

h = Hash(String, String).from_yaml(yaml) # => {"x" => "abc", "y" => "abc"}
h["x"].same?(h["y"])                     # => true

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions