Skip to content

MessagePackGenerator (mpc.exe) ignore generic type definition, that defined outside target project. #1111

@gsino

Description

@gsino

environments:

  • Version used: 2.2.60
  • Runtime: .NET Core

Bug description

Generate process does not collect generic type definition, that defined outside target project.

Repro steps

  1. There are two projects, MyApp.Models.csproj and MyApp.Shared.csproj. MyApp.Models refers to MyApp.Shared.
    MyApp.Models has generic type definition, that MyApp.Shared indirectly references.
in MyApp.Models.csproj
[MessagePackObject()]
public class UserInventoryChanges
{
    [Key(0)] public InventoryChangedValue<int> Items { get; set; }
    [Key(1)] public InventoryChangedValue<Currency> Currencies { get; set; }
}

[MessagePackObject()]
public class InventoryChangedValue<T>
{
    [Key(0)] public T Value { get; set; }
}
in MyApp.Shared.csproj
[MessagePackObject()]
public class Dummy
{
    [Key(0)] public UserInventoryChanges _field { get; set; }
}
  1. Run mpc.exe against MyApp.Shared.csproj.
    dotnet mpc -i ./MyApp.Shared.csproj -o ./MessagePackGenerated/GeneratedResolver.cs -c MPC -r GeneratedResolver
    The generated file does not contain the InventoryChangedValueFormatter<T>.

Expected behavior

The generated file contains formatter for generic type definition, that defined outside target project (InventoryChangedValueFormatter<T>).

Actual behavior

not so.

Additional context

I got the expected result by modifying it to collect the OriginalDefinition when it was a generic type.

https://github.com/neuecc/MessagePack-CSharp/blob/d61346e6ba810c5019a6f3a01cbe3055ca15c7e3/src/MessagePack.GeneratorCore/CodeAnalysis/TypeCollector.cs#L583-L596

// Generic types
if (type.IsDefinition)
{
    this.CollectGenericUnion(type);
    this.CollectObject(type);
    return;
}
else
{
    // Collect substituted types for the properties and fields.
    // NOTE: It is used to register formatters from nested generic type.
    //       However, closed generic types such as `Foo<string>` are not registered as a formatter.
    GetObjectInfo(type);

    // Collect generic type definition, that is not collected when it is defined outside target project.
    CollectCore(type.OriginalDefinition);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions