Dahomey.Cbor icon indicating copy to clipboard operation
Dahomey.Cbor copied to clipboard

Deserialization of abstract record class fails

Open rmja opened this issue 3 years ago • 0 comments

Consider this case:

public abstract record class Animal(DateTime Timestamp)
    {
    }

[CborDiscriminator("dog")]
    public record class Dog(string Color, DateTime Timestamp) : Ingress(Timestamp)
    {
        // This example fails if this empty constructor is not present
        private Dog() : this("black", default)
        {
        }
    }

public static class DataIngressCborOptions
    {
        public static readonly CborOptions Value = new();

        static DataIngressCborOptions()
        {
            Value.Registry.DiscriminatorConventionRegistry.RegisterConvention(new AttributeBasedDiscriminatorConvention<string>(Value.Registry));
            Value.Registry.DiscriminatorConventionRegistry.RegisterType(typeof(Dog));
        }
    }

Cbor.Deserialize<Animal[]>(..., DataIngressCborOptions.Value);

In the example, if the Dog ctor is not defined, _constructor is never assigned in ObjectConverter causing the exception A CreatorMapping should be defined for interfaces or abstract classes.

rmja avatar Jun 30 '22 07:06 rmja