YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Anchor not found when referenced in double nested structure

Open MortenChristiansen opened this issue 5 years ago • 6 comments

There seems to be a problem with the way anchors are resolved. I get the following exception when running the test shown below: YamlDotNet.Core.AnchorNotFoundException: '(Line: 18, Col: 14, Idx: 264) - (Line: 18, Col: 21, Idx: 271): Anchor 'goblin' not found'. If I remove the last line of the yaml definition, it does not throw any exception, which means that the two outermost anchors are resolved correctly. It seems that for some reason, the third level of nesting breaks the ability for the deserializer to resolve the anchor.

[Fact]
public void Test()
{
    var yml = @"
monsters:
- monster: &goblin
  name: Goblin
  attackType: Piercing

monster: *goblin

name: Forest
levels:
- level:
  name: Clearing
  monster: *goblin
  locations:
  - location:
    name: Location 1
    description: A nice location
    monster: *goblin";

    var deserializer = new DeserializerBuilder()
                .IgnoreUnmatchedProperties()
                .WithNamingConvention(new CamelCaseNamingConvention())
                .Build();

    var areaRecord = deserializer.Deserialize<AreaRecord>(yml);
    Assert.NotNull(areaRecord );
}

public class AreaRecord
{
    public string Name { get; set; }
    public List<LevelRecord> Levels { get; set; } = new List<LevelRecord>();
}

public class LevelRecord
{
    public string Name { get; set; }
    public List<dynamic> Locations { get; set; } = new List<dynamic>();
}

MortenChristiansen avatar May 05 '19 11:05 MortenChristiansen