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

Anchor missing for sibling nested objects #97

Closed
RKeelan opened this issue May 5, 2022 · 2 comments
Closed

Anchor missing for sibling nested objects #97

RKeelan opened this issue May 5, 2022 · 2 comments
Labels

Comments

@RKeelan
Copy link

RKeelan commented May 5, 2022

The code below serializes an OuterClass that contains a list of two InnerClasses. Both InnerClasses have a reference to the same array. In the serialization, the first InnerClass's Arrayhas no anchor, but the second InnerClass's Array nevertheless uses an alias (which is presumably supposed to refer to the first Array).

I tested this with SharpYaml 1.9.0.

Serialization:

!Yaml.OuterClass
InnerObjects:
  - Array:
      - 1
      - 2
  - Array: *o3

Code:

using NUnit.Framework;
using SharpYaml.Serialization;
using System.Collections.Generic;

namespace Yaml {
    [TestFixture]
    public class Test {
        [Test]
        public void Anchor()
        {
            var array = new int[] { 1, 2 };
            var expected = new OuterClass() {
                InnerObjects = new List<InnerClass>()
            };
            for (int i = 0; i < 2; i++) {
                var inner = new InnerClass {
                    Array = array
                };
                expected.InnerObjects.Add(inner);
            }

            var settings = new SerializerSettings() { EmitDefaultValues = true, EmitAlias = true };
            settings.RegisterAssembly(typeof(OuterClass).Assembly);
            var serializer = new Serializer(settings);

            string serialString = serializer.Serialize(expected);
           // This line throws an exception
            var actual = serializer.Deserialize(serialString);
        }

    }
    public class OuterClass {
        public List<InnerClass> InnerObjects { get; set; }
    }
    public class InnerClass {
        public int[] Array { get; set; }
    }
}
@xoofx xoofx added the bug label May 8, 2022
xoofx added a commit that referenced this issue May 8, 2022
@xoofx
Copy link
Owner

xoofx commented May 8, 2022

Thanks. Seems you were the first one to try to serialize a same array instance 🙂
This should be fixed by commit dcd5e74

@xoofx xoofx closed this as completed May 8, 2022
@RKeelan
Copy link
Author

RKeelan commented May 10, 2022

That's great, thank you!

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

No branches or pull requests

2 participants