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

Missing MsgPack003 error when using class from other project

Open treziac opened this issue 6 months ago • 1 comments

Bug description

When a class annotated with MessagePackObject define a property with a type defined in another project, this type is not checked to be properly annotated with MessagePackObject.

Repro steps

Create a first csproj, with a class without attributes

    public class Foo
    {
        public required int Id { get; init; }
    }

Create a second project in the same solution, referencing the first one, with in it

public class Program
{
    public static void Main()
    {
        MessagePackSerializer.Serialize(new Bar());
    }
}

[MessagePackObject]
public class Bar
{
    public Bar()
    {
        Foo = new Foo { Id = 1 };
    }

    [Key(0)]
    public Foo Foo { get; set; }
}

Expected behavior

A compilation error with MsgPack003 should be raised

Actual behavior

Code compiles, and an exception MessagePack.FormatterNotRegisteredException: SharedData.Foo is not registered in resolver: MessagePack.Resolvers.StandardResolver is thrown at runtime

  • Version used: 3.1.4 and master
  • Runtime: tested on .NET Core 9 & net472

treziac avatar Oct 10 '25 14:10 treziac

That's by design, I'd say.

We assume people will need to reference types from other assemblies that they do not control and can't annotate. It's your responsibility to apply appropriate resolvers/formatters to be able to handle those types if they are not annotated.

AArnott avatar Oct 10 '25 23:10 AArnott