Skip to content

"Cannot capture a buffered reader." with two-level polymorphic #125

Description

@fthmko

version: 2.4.1
.net version: 10.0.300

As min code shown below, “Cannot capture a buffered reader.” is raised unexpected.

using System;
using System.Text.Json.Serialization;
using Tomlyn;
using Tomlyn.Serialization;

namespace Test {
    [TomlSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower)]
    [TomlDerivedTypeMapping(typeof(DepartLevel), typeof(Depart1), "dd1")]
    [TomlDerivedTypeMapping(typeof(GroupLevel), typeof(Group2), "gg2")]
    [TomlSerializable(typeof(MyConfig))]
    public partial class MyConfigContext : TomlSerializerContext {}

    public record MyConfig {
        public required DepartLevel Depart { get; set; }
    }

    public abstract record DepartLevel {
        public required string Name { get; set; }
        public required GroupLevel Group { get; set; }
    }

    public sealed record Depart1 : DepartLevel {
        public required string Field1 { get; set; }
    }

    public abstract record GroupLevel {
        public required string Name { get; set; }
    }

    public sealed record Group2 : GroupLevel {
        public required string Field2 { get; set; }
    }

    internal class Test {
        static string toml = """
[depart]
"$type" = "dd1"
name = "this is depart1"
field1 = "depart 1 field"

[depart.group]
"$type" = "gg2"
name = "this is group 2"
field2 = "group 2 field"
""";

        [STAThread]
        public static void Main(string[] args) {
            TomlSerializer.Deserialize(toml, MyConfigContext.Default.MyConfig);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions