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

Aliases do not resolve correctly in Sequences #933

Closed
MetaFight opened this issue Jun 17, 2024 · 1 comment · Fixed by #938
Closed

Aliases do not resolve correctly in Sequences #933

MetaFight opened this issue Jun 17, 2024 · 1 comment · Fixed by #938

Comments

@MetaFight
Copy link
Contributor

MetaFight commented Jun 17, 2024

https://github.com/aaubry/YamlDotNet/blob/23991bd3d22009f7478e9e5c3ac71b106a8da3b7/YamlDotNet/Serialization/NodeDeserializers/ArrayNodeDeserializer.cs#L48C13-L53C1

The ArrayNodeDeserializer leverages CollectionNodeDeserializer.DeserializeHelper to deserialize nodes, however, because it doesn't know the final size of the array being deserialized it uses an ArrayList as a temporary result bucket. Because the IList result it passed into CollectionNodeDeserializer.DeserializeHelper is not the final array, any ValuePromises that get eventually resolved here are not reflected in the final array.

I have been able to get around this by using the attached replacement AnchorSafeArrayNodeDeserializer.cs.

You will need to configure your deserialisation builder as follows to use it:

var namingConvention = YamlDotNet.Serialization.NamingConventions.NullNamingConvention.Instance;

var builder =
    new DeserializerBuilder()
        .WithNamingConvention(namingConvention)
        .WithNodeDeserializer(
            inner => new AnchorSafeArrayNodeDeserializer(namingConvention),
            s => s.InsteadOf<ArrayNodeDeserializer>());

It should be noted that the provided AnchorSafeArrayNodeDeserializer uses the BCL ArrayList rather than the version originally defined inline. As such, this solution will probably need to be adjusted if used in a PR.

I would create a PR myself, but I just don't have the time at the moment. I hope this still helps people.

If it helps, I believe the following yaml should reproduce the original issue:

items:
- &g000_FirstGroup
  name: FirstGroup
- &g001_SecondGroup
  name: SecondGroup
  child: &c000_FirstChild
    name: FirstChild
    groups:
    - *g001_SecondGroup
    - *g000_FirstGroup
    - &g002_ThirdGroup
      name: thirdGroup

When deserialized the obj.items[1].groups array will have a null first entry.

@MetaFight MetaFight changed the title ArrayNodeDeserializer does not handle ValuePromise resolution correctly Aliases do not resolve correctly in Sequences Jun 17, 2024
@MetaFight
Copy link
Contributor Author

MetaFight commented Jun 19, 2024

Some work obligations got postponed so I decided to use the spare time to prepare a PR.

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