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

Duplicate contents represented by Anchor are actually the same reference object #1044

Closed
Polar-Pumpkin opened this issue Feb 27, 2025 · 1 comment

Comments

@Polar-Pumpkin
Copy link

Describe the bug
I tried to use anchor to simplify the repetitive content in my yaml configuration. I thought anchor just represented repeated yaml text content, but I found that the objects represented by anchor all refer to the same object, rather than repeatedly creating different (reference) objects as I imagined.

If the deserialized data is read-only, there should be no problem, but occasionally I want to store some states in the deserialized object. At this time, some conflicts will occur due to they are actually the same reference object.

I want to know if this behavior is designed? Is my understanding of anchor wrong?
Thank you very much for your answer and help. ❤

To Reproduce

using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace YamlAnchorTest;

public class Configuration {
    public List<Executor> Executors { get; set; }
}

public class Executor {
    public string Name { get; set; }
    public List<Filter> Filters { get; set; }
}

public class Filter {
    public string Type { get; set; }
    public int Value { get; set; }
}

internal static class Program {
    private static void Main(string[] args) {
        var document =
            """
            executors:
              - name: a
                filters: &shared
                  - type: foo
                    value: 1
                  - type: bar
                    value: 2
              - name: b
                filters: *shared
            """;
        var deserializer = new DeserializerBuilder()
            .WithNamingConvention(HyphenatedNamingConvention.Instance)
            .WithEnumNamingConvention(HyphenatedNamingConvention.Instance)
            .Build();
        var conf = deserializer.Deserialize<Configuration>(document);
        Console.WriteLine(ReferenceEquals(conf.Executors[0].Filters, conf.Executors[1].Filters));
        // Output: true
        // If I add an item to the Filters of one Executor at this time, the other Executor will also changed
    }
}
@Polar-Pumpkin
Copy link
Author

I'm very sorry to bother, I unexpectedly opened two same issues due to laggy network.

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

1 participant