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

Support IReadOnlyCollection #1008

Open
BenjaminBrienen opened this issue Nov 15, 2024 · 2 comments
Open

Support IReadOnlyCollection #1008

BenjaminBrienen opened this issue Nov 15, 2024 · 2 comments

Comments

@BenjaminBrienen
Copy link

YamlDotNet.Core.YamlException: No node deserializer was able to deserialize the node into type System.Collections.Generic.IReadOnlyCollection

Properties should not return arraysCA1819

The recommended solution is IReadOnlyCollection, especially if the intent is to have an immutable record.

@EdwardCooke
Copy link
Collaborator

Can you share some example code and yaml that you’re trying to deserialize? I think, may be wrong, but we may already support read only collections. Can’t entirely remember

@BenjaminBrienen
Copy link
Author

public record Extends(string Template, object Parameters)
{
    public Extends() : this(default!, default!) { }
}
public record Parameter(string Name, string DisplayName, string Type, string Default)
{
    public Parameter() : this(default!, default!, default!, default!) { }
}

#pragma warning disable CA1724 // "Resources" is correct
public record Resources(RepositoryItem[] Repositories)
{
    public Resources() : this([]) { }
}
#pragma warning restore CA1724


public record RepositoryItem(string Repository, string Type, string Name, string Ref)
{
    public RepositoryItem() : this(default!, default!, default!, default!) { }
}

public record Trigger(bool Batch, Branches Branches)
{
    public Trigger() : this(default!, default!) { }
}

public record Branches(string[] Include)
{
    public Branches() : this([]) { }
}

public record Pipeline(Trigger Trigger, Resources Resources, Parameter[] Parameters, Extends Extends)
{
    public Pipeline() : this(default!, default!, default!, default!) { }

    public static Pipeline Parse(string yaml)
    {
        var deserializer = new DeserializerBuilder()
            .WithNamingConvention(CamelCaseNamingConvention.Instance)
            .IgnoreUnmatchedProperties()
            .Build();
        return deserializer.Deserialize<Pipeline>(yaml);
    }
}

You can see here that I had to replace IReadOnlyCollections with arrays and add default constructors. Otherwise, you get the error in the OP. The default constructors can be a seperate issue.

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