Compile Error When Using Default Interface Implementation of IMessagePackFormatter<T> with Source Generator

オープン
#2,211 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp
領域
tooling

調査の方向性

提供された .NET 8 の再現コードから始め、MessagePack ソースジェネレーターによる formatter の検出と、CustomMessageFormatter のインターフェイスメソッド解決を追跡してください。IMessagePackFormatter の既定実装が生成コードでどのように表現されるかを確認し、示されている formatter パターンの回帰テストカバレッジを追加してください。プロジェクトがコンパイルされ、生成された serializer が継承された Serialize メソッドと Deserialize メソッドを受け入れれば完了です。

索引モデルが issue の本文から書いたものです。

説明

When implementing IMessagePackFormatter via an interface that provides default implementations for Serialize and Deserialize, a compile-time error occurs if a concrete formatter class relies on those default methods and is used with the MessagePack source generator.

[MessagePackObject]
public class CustomMessage
{
    [Key(0)]
    public string Content { get; set; } = "";

    [Key(1)]
    public string CustomData { get; set; } = "";
}

public interface ICustomSerializableFormatter<TFrom, TTo> : IMessagePackFormatter<TFrom>
{
    TTo ConvertTo(TFrom value);
    TFrom ConvertFrom(TTo value);

    void IMessagePackFormatter<TFrom>.Serialize(ref MessagePackWriter writer, TFrom value, MessagePackSerializerOptions options)
    {
        if (value == null)
        {
            writer.WriteNil();
            return;
        }
        options.Resolver.GetFormatterWithVerify<TTo>().Serialize(ref writer, ConvertTo(value), options);
    }

    TFrom IMessagePackFormatter<TFrom>.Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
    {
        return ConvertFrom(options.Resolver.GetFormatterWithVerify<TTo>().Deserialize(ref reader, options));
    }
}

public class CustomMessageFormatter : ICustomSerializableFormatter<CustomMessage, (string, string)>
{
    public (string, string) ConvertTo(CustomMessage value)
    {
        return (value.Content, value.CustomData);
    }

    public CustomMessage ConvertFrom((string, string) value)
    {
        return new CustomMessage
        {
            Content = value.Item1,
            CustomData = value.Item2
        };
    }
}

If CustomMessageFormatter is assigned as the custom formatter for CustomMessage (e.g. via MessagePackFormatter attribute or resolver registration), the MessagePack source generator fails with a compile-time error.

Although the interface provides default implementations for Serialize and Deserialize, and the implementing class doesn't need to override them explicitly, the source generator does not recognize those default methods and treats them as missing.

✅ Expected Behavior
The source generator should recognize and support default interface method implementations of IMessagePackFormatter, allowing formatter classes to inherit them without explicitly overriding the methods.

💡 Additional Info
This issue occurs outside of Unity, in a standard .NET 8 project.

MessagePack C# version: (3.14)

.NET SDK version: (.net 8)

The issue seems to stem from the generator not handling default interface method resolution correctly.

主要言語
C#
スター
6.8k
フォーク
783
平均マージ
19時間 47分
マージ済み PR(30日)
3

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MessagePack-CSharp/MessagePack-CSharp のほかの issue

MessagePack-CSharp/MessagePack-CSharp の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。