Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YamlDotNet is not parsing anchors correctly #627

Closed
darkguy2008 opened this issue Aug 10, 2021 · 1 comment
Closed

YamlDotNet is not parsing anchors correctly #627

darkguy2008 opened this issue Aug 10, 2021 · 1 comment

Comments

@darkguy2008
Copy link

darkguy2008 commented Aug 10, 2021

Describe the bug
Objects referencing anchors come out as null.

To Reproduce

using System.IO;
using YamlDotNet.Core;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace compiler
{
    class Program
    {
        static void Main(string[] args)
        {
            string yaml = @"
controls:
  - control: &panel1
    type: panel
    background: red

  - control: &panel2
    type: panel
    background: blue

structure:
  - *panel1
  - *panel2
            ";
            var ds = new DeserializerBuilder()
                .WithNamingConvention(CamelCaseNamingConvention.Instance)
                .IgnoreUnmatchedProperties()
                .Build();
            var f = ds.Deserialize<object>(
                new MergingParser(new Parser(new StringReader(yaml))));
        }
    }
}

Result: Structure items are null:

image

Changing the structure to:

structure:
  - <<: *panel1
  - <<: *panel2

Throws an exception:

Exception has occurred: CLR/System.Collections.Generic.KeyNotFoundException
An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in System.Private.CoreLib.dll: 'The given key 'panel1' was not present in the dictionary.'
   at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at YamlDotNet.Core.MergingParser.ParsingEventCollection.FromAnchor(AnchorName anchor)
   at YamlDotNet.Core.MergingParser.GetMappingEvents(AnchorName anchor)
   at YamlDotNet.Core.MergingParser.HandleAnchorAlias(LinkedListNode`1 node, LinkedListNode`1 anchorNode, AnchorAlias anchorAlias)
   at YamlDotNet.Core.MergingParser.HandleMerge(LinkedListNode`1 node)
   at YamlDotNet.Core.MergingParser.Merge()
   at YamlDotNet.Core.MergingParser.MoveNext()
   at YamlDotNet.Core.ParserExtensions.Accept[T](IParser parser, T& event)
   at YamlDotNet.Core.ParserExtensions.TryConsume[T](IParser parser, T& event)
   at YamlDotNet.Serialization.Deserializer.Deserialize(IParser parser, Type type)
   at YamlDotNet.Serialization.Deserializer.Deserialize[T](IParser parser)

Expected result

Structure should contain the same items shown in the Controls key in the screenshot above.

@EdwardCooke
Copy link
Collaborator

This makes sense. The anchor is set to the content of control. Which is empty. And according to yaml spec, empty means null.

The following YAML:

controls:
  - control: &panel1 hello1
    type: panel
    background: red

  - control: &panel2 hello2
    type: panel
    background: blue

structure:
  - *panel1
  - *panel2

Looks like this:
image

The following yaml

controls:
  - &panel1
    control: hello1
    type: panel
    background: red

  - &panel2
    control: hello2
    type: panel
    background: blue

structure:
  - *panel1
  - *panel2

looks like this:

image

I'm going to close this issue. If you need more assistance, or disagree, feel free to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants