diff --git a/src/MessagePack.SourceGenerator/CodeAnalysis/TypeCollector.cs b/src/MessagePack.SourceGenerator/CodeAnalysis/TypeCollector.cs index 49cb689ed..10a11fc87 100644 --- a/src/MessagePack.SourceGenerator/CodeAnalysis/TypeCollector.cs +++ b/src/MessagePack.SourceGenerator/CodeAnalysis/TypeCollector.cs @@ -455,7 +455,7 @@ private bool CollectArray(IArrayTypeSymbol array) var info = GenericSerializationInfo.Create(array, this.options.Generator.Resolver) with { - Formatter = new QualifiedTypeName("MsgPack::Formatters", null, TypeKind.Class, formatterName, ImmutableArray.Create(elementTypeName.GetQualifiedName())), + Formatter = new QualifiedTypeName("MsgPack::Formatters", null, TypeKind.Class, formatterName, ImmutableArray.Create(elementTypeName.GetQualifiedName(genericStyle: GenericParameterStyle.Arguments))), }; this.collectedGenericInfo.Add(info); return true; diff --git a/src/MessagePack.SourceGenerator/Utils/AnalyzerUtilities.cs b/src/MessagePack.SourceGenerator/Utils/AnalyzerUtilities.cs index 6c38d7817..5760232dc 100644 --- a/src/MessagePack.SourceGenerator/Utils/AnalyzerUtilities.cs +++ b/src/MessagePack.SourceGenerator/Utils/AnalyzerUtilities.cs @@ -128,6 +128,12 @@ internal static IEnumerable ResolverSymbolToInstanceExpression(SemanticM } } + // If the resolver has GeneratedMessagePackResolverAttribute, it has static Instance property + if (r.GetAttributes().Any(x => x.AttributeClass?.Name == GeneratedMessagePackResolverAttributeName && x.AttributeClass?.ContainingNamespace.GetFullNamespaceName() == AttributeNamespace)) + { + return $"{r.GetCanonicalTypeFullName()}.Instance"; + } + // Fallback to instantiating the resolver, if a constructor is available. if (r.InstanceConstructors.FirstOrDefault(c => c.Parameters.Length == 0) is IMethodSymbol ctor) { diff --git a/tests/MessagePack.SourceGenerator.ExecutionTests/GeneratedCompositeResolverTests.cs b/tests/MessagePack.SourceGenerator.ExecutionTests/GeneratedCompositeResolverTests.cs index 60ec4d9f6..e5e835aa6 100644 --- a/tests/MessagePack.SourceGenerator.ExecutionTests/GeneratedCompositeResolverTests.cs +++ b/tests/MessagePack.SourceGenerator.ExecutionTests/GeneratedCompositeResolverTests.cs @@ -16,7 +16,7 @@ public void CanFindFormatterFromVariousResolvers() namespace Tests { - [CompositeResolver(typeof(NativeGuidResolver), typeof(NativeDecimalResolver))] + [CompositeResolver(typeof(GeneratedMessagePackResolver), typeof(NativeGuidResolver), typeof(NativeDecimalResolver))] internal partial class MyGeneratedCompositeResolver { } diff --git a/tests/MessagePack.SourceGenerator.Tests/GenerationTests.cs b/tests/MessagePack.SourceGenerator.Tests/GenerationTests.cs index 3ce8fdf53..72aa58348 100644 --- a/tests/MessagePack.SourceGenerator.Tests/GenerationTests.cs +++ b/tests/MessagePack.SourceGenerator.Tests/GenerationTests.cs @@ -266,12 +266,16 @@ public async Task ArrayTypedProperty() { string testSource = """ using MessagePack; +using System.Collections.Generic; [MessagePackObject] internal class ContainerObject { [Key(0)] internal SubObject[] ArrayOfCustomObjects { get; set; } + + [Key(1)] + internal List[] ArrayOfCustomObjectList { get; set; } } [MessagePackObject] diff --git a/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/Formatters.MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter.g.cs b/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/Formatters.MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter.g.cs index 484995423..b50c434c7 100644 --- a/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/Formatters.MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter.g.cs +++ b/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/Formatters.MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter.g.cs @@ -21,8 +21,9 @@ public void Serialize(ref MsgPack::MessagePackWriter writer, global::ContainerOb } MsgPack::IFormatterResolver formatterResolver = options.Resolver; - writer.WriteArrayHeader(1); + writer.WriteArrayHeader(2); MsgPack::FormatterResolverExtensions.GetFormatterWithVerify(formatterResolver).Serialize(ref writer, value.ArrayOfCustomObjects, options); + MsgPack::FormatterResolverExtensions.GetFormatterWithVerify[]>(formatterResolver).Serialize(ref writer, value.ArrayOfCustomObjectList, options); } public global::ContainerObject Deserialize(ref MsgPack::MessagePackReader reader, MsgPack::MessagePackSerializerOptions options) @@ -44,6 +45,9 @@ public void Serialize(ref MsgPack::MessagePackWriter writer, global::ContainerOb case 0: ____result.ArrayOfCustomObjects = MsgPack::FormatterResolverExtensions.GetFormatterWithVerify(formatterResolver).Deserialize(ref reader, options); break; + case 1: + ____result.ArrayOfCustomObjectList = MsgPack::FormatterResolverExtensions.GetFormatterWithVerify[]>(formatterResolver).Deserialize(ref reader, options); + break; default: reader.Skip(); break; diff --git a/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/MessagePack.GeneratedMessagePackResolver.g.cs b/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/MessagePack.GeneratedMessagePackResolver.g.cs index 49ab0f1f4..2e67c78c9 100644 --- a/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/MessagePack.GeneratedMessagePackResolver.g.cs +++ b/tests/MessagePack.SourceGenerator.Tests/Resources/ArrayTypedProperty/MessagePack.GeneratedMessagePackResolver.g.cs @@ -39,11 +39,13 @@ static FormatterCache() private static class GeneratedMessagePackResolverGetFormatterHelper { - private static readonly global::System.Collections.Generic.Dictionary closedTypeLookup = new(3) + private static readonly global::System.Collections.Generic.Dictionary closedTypeLookup = new(5) { { typeof(global::SubObject[]), 0 }, - { typeof(global::ContainerObject), 1 }, - { typeof(global::SubObject), 2 }, + { typeof(global::System.Collections.Generic.List[]), 1 }, + { typeof(global::System.Collections.Generic.List), 2 }, + { typeof(global::ContainerObject), 3 }, + { typeof(global::SubObject), 4 }, }; internal static object GetFormatter(global::System.Type t) @@ -53,8 +55,10 @@ internal static object GetFormatter(global::System.Type t) return closedKey switch { 0 => new MsgPack::Formatters.ArrayFormatter(), - 1 => new global::MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter(), - 2 => new global::MessagePack.GeneratedMessagePackResolver.SubObjectFormatter(), + 1 => new MsgPack::Formatters.ArrayFormatter>(), + 2 => new MsgPack::Formatters.ListFormatter(), + 3 => new global::MessagePack.GeneratedMessagePackResolver.ContainerObjectFormatter(), + 4 => new global::MessagePack.GeneratedMessagePackResolver.SubObjectFormatter(), _ => null, // unreachable }; }