YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Support "init" property accessor with "required" modifier in YamlDotNet Source Generation for AOT

Open dje1990 opened this issue 2 months ago • 0 comments

Please add support for the init property accessor with the required modifier in YamlDotNet Source Generation for AOT

E.g. for a given POCO that I want to map YAML to:

public sealed record BooleanResult
{
    public required bool result { get; init; }
}

the generated source code in YamlDotNetAutoGraph.g.cs tries to write to the property but the property has the init accessor instead of the set accessor so a compliation error occurs: Init-only property or indexer 'BooleanResult.result' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.

// from YamlDotNetAutoGraph.g.cs
switch (propertyName)
{
    case "result": v.result = (System.Boolean)value; return;
    default: throw new ArgumentOutOfRangeException("propertyName", $"{propertyName} does not exist or is not settable");
}

equally because the required modifier is used all properties must be set when the constructor is called else a compliation error occurs: Required member 'BooleanResult.result' must be set in the object initializer or attribute constructor.

// from YamlDotNetAutoGraph.g.cs
class StaticObjectFactory : YamlDotNet.Serialization.ObjectFactories.StaticObjectFactory
{
    public override object Create(Type type)
    {
        if (type == typeof(MyNamespace.BooleanResult)) return new MyNamespace.BooleanResult();

dje1990 avatar Nov 09 '25 01:11 dje1990