No possibility to disable source generation for UnionAttribute
Author: krishkatCreated Apr 10, 2025Updated Sep 11, 2026
Bug description
With MessagePack v3 it is basically impossible to use string type names for UnionAttribute. As the source generator will nevertheless generate a Formatter which is then used by the standard resolver.
Repro steps
Create an interface like this:
[Union(0, "MyNamespace.SampleClass")] // MsgPack008: The source generator only supports UnionAttribute with a Type argument
public interface ISampleInterface
{
int SampleProperty { get; set; }
}Expected behavior
I expect no formatter to be generated by source generation and instead as in v2 generation should happen at runtime, which then supports string type names.
Actual behavior
MessagePack generates the following formatter, which then always causes null to be serialized:
// <auto-generated />
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649
using MsgPack = global::MessagePack;
namespace MessagePack {
partial class GeneratedMessagePackResolver {
internal sealed class ISampleInterfaceFormatter: MsgPack::Formatters.IMessagePackFormatter<global::ISampleInterface>
{
private readonly global::System.Collections.Generic.Dictionary<global::System.RuntimeTypeHandle, global::System.Collections.Generic.KeyValuePair<int, int>> typeToKeyAndJumpMap;
private readonly global::System.Collections.Generic.Dictionary<int, int> keyToJumpMap;
public ISampleInterfaceFormatter()
{
this.typeToKeyAndJumpMap = new global::System.Collections.Generic.Dictionary<global::System.RuntimeTypeHandle, global::System.Collections.Generic.KeyValuePair<int, int>>(0, MsgPack::Internal.RuntimeTypeHandleEqualityComparer.Default)
{
};
this.keyToJumpMap = new global::System.Collections.Generic.Dictionary<int, int>(0)
{
};
}
public void Serialize(ref MsgPack::MessagePackWriter writer, global::ISampleInterface value, MsgPack::MessagePackSerializerOptions options)
{
global::System.Collections.Generic.KeyValuePair<int, int> keyValuePair;
if (value != null && this.typeToKeyAndJumpMap.TryGetValue(value.GetType().TypeHandle, out keyValuePair))
{
writer.WriteArrayHeader(2);
writer.WriteInt32(keyValuePair.Key);
switch (keyValuePair.Value)
{
default:
break;
}
return;
}
writer.WriteNil();
}
public global::ISampleInterface Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
{
return null;
}
if (reader.ReadArrayHeader() != 2)
{
throw new global::System.InvalidOperationException("Invalid Union data was detected. Type:global::ISampleInterface");
}
options.Security.DepthStep(ref reader);
var key = reader.ReadInt32();
if (!this.keyToJumpMap.TryGetValue(key, out key))
{
key = -1;
}
global::ISampleInterface result = null;
switch (key)
{
default:
reader.Skip();
break;
}
reader.Depth--;
return result;
}
}
}
}- Version used: 3.1.3
- Runtime: .NET 8
Additional context
- In some cases it is impossible to use
typeof()as we have an interface/abstract class in a parent project but then would need to include all implementations of all child projects. - From a design standpoint, it would also be fine to include an
SuppressSourceGenerationproperty as it is done by theMessagePackObjectAttribute. - The only workaround is to use a Resolver without SourceGeneration support, which eliminates most benefits gained from using v3.
Source: MessagePack-CSharp/MessagePack-CSharp