MessagePack-CSharp icon indicating copy to clipboard operation
MessagePack-CSharp copied to clipboard

MsgPack006 error when using custom formatter for generic types and MessagePack.Union

Open hyabean opened this issue 8 months ago • 0 comments

Bug description

MsgPack006 error when using generic class in custom formatter when class is GenericType and subType of a MessagePack.UnionAttribute marked abstruct class.

    // ? MsgPack006 Type must be of IMessagePackFormatter: {0}
    [MessagePackFormatter(typeof(MessagePackEmDataColumnFormatter<>))]  
    [MessagePackObject]
    public partial class CustomDataColumn<T> : DataColumnBase
    {
        [MessagePack.Key(0)]
        public List<T> Values { get; set; }
    }

    [Union(0, typeof(CustomDataColumn<int>))]
    [Union(1, typeof(CustomDataColumn<double>))]
    public abstract partial class DataColumnBase
    {
    }


    public sealed class MessagePackEmDataColumnFormatter<T> : IMessagePackFormatter<CustomDataColumn<T>>
    {
        public void Serialize(ref MessagePackWriter writer, CustomDataColumn<T> value, MessagePackSerializerOptions options)
        {
            Console.WriteLine("Custom ObjectPoolDataColumnFormatter Serialize called");
        }

        public CustomDataColumn<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            Console.WriteLine("Custom ObjectPoolDataColumnFormatter Deserialize called");
            var ____result = default(CustomDataColumn<T>);
            return ____result;
        }
    }

Repro steps

MessagePackUnionAndFormatterDemo

Expected behavior

The code should compile without error MsgPack006 .

Actual behavior

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	MsgPack006	Type must be of IMessagePackFormatter: {0}	MemoryPackDemo	CustomDataColumn.cs	8
```		

MsgPack006 error occured. Building can success if I suppress the warning

Version used: MessagePack v3.1.3
Runtime: .Net 8.0

#### Additional context

hyabean avatar May 15 '25 17:05 hyabean