From 29a13b78bae26ad5c41395ac4842f1d902f5c82d Mon Sep 17 00:00:00 2001 From: latonz Date: Wed, 18 Nov 2026 10:35:16 +0100 Subject: [PATCH] fix: exclude open generic mappings from runtime dispatch --- .../RuntimeTargetTypeMappingBodyBuilder.cs | 7 ++++- .../Mapping/UserMethodTest.cs | 22 ++++++++++++++++ ...tDispatcherShouldWork#Mapper.g.verified.cs | 26 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.WithGenericUserImplementedExistingTargetAndExistingTargetDispatcherShouldWork#Mapper.g.verified.cs diff --git a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/RuntimeTargetTypeMappingBodyBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/RuntimeTargetTypeMappingBodyBuilder.cs index dd17d8637d..e96f27b6ba 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/RuntimeTargetTypeMappingBodyBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBodyBuilders/RuntimeTargetTypeMappingBodyBuilder.cs @@ -64,8 +64,13 @@ private static IEnumerable GetUserMappingCandidates(Map .Where(x => x is not UserDefinedNewInstanceRuntimeTargetTypeMapping) .OfType(); + // Generic dispatchers and generic user-implemented templates are open generic methods + // which cannot be invoked directly in a type switch arm. + // Concrete instantiations of the templates are registered as separate mappings once they are built. private static IEnumerable GetExistingTargetUserMappingCandidates(MappingBuilderContext ctx) => - ctx.ExistingTargetUserMappings.Where(x => x is not UserDefinedExistingTargetGenericTypeMapping); + ctx.ExistingTargetUserMappings.Where(x => + x is not (UserDefinedExistingTargetGenericTypeMapping or GenericUserImplementedExistingTargetMethodMapping) + ); private static void BuildExistingTargetMappingBody( MappingBuilderContext ctx, diff --git a/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs b/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs index 9c2ebef1bf..351a170ab5 100644 --- a/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs @@ -1316,6 +1316,28 @@ private void MapGeneric([MappingTarget] TTarget target, TSourc ); } + [Fact] + public Task WithGenericUserImplementedExistingTargetAndExistingTargetDispatcherShouldWork() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + """ + public partial void Map(TSource source, TTarget target); + + public partial void MapA(A source, B target); + + private void MapContainer(IContainer source, IContainer target) + { + Map, IContainer>(source, target); + } + """, + "public interface IContainer { T Value { get; set; } }", + "class A { public int Value { get; set; } }", + "class B { public int Value { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + [Fact] public Task WithGenericUserImplementedExistingTargetAndReferenceHandling() { diff --git a/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.WithGenericUserImplementedExistingTargetAndExistingTargetDispatcherShouldWork#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.WithGenericUserImplementedExistingTargetAndExistingTargetDispatcherShouldWork#Mapper.g.verified.cs new file mode 100644 index 0000000000..573a555343 --- /dev/null +++ b/test/Riok.Mapperly.Tests/_snapshots/UserMethodTest.WithGenericUserImplementedExistingTargetAndExistingTargetDispatcherShouldWork#Mapper.g.verified.cs @@ -0,0 +1,26 @@ +//HintName: Mapper.g.cs +// +#nullable enable +public partial class Mapper +{ + [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] + public partial void Map(TSource source, TTarget target) + { + if (source == null || target == null) + return; + switch (source, target) + { + case (global::A source1, global::B target1): + MapA(source1, target1); + break; + default: + throw new global::System.ArgumentException($"Cannot map {source.GetType()} to {target.GetType()} as there is no known type mapping", nameof(source)); + } + } + + [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] + public partial void MapA(global::A source, global::B target) + { + target.Value = source.Value; + } +} \ No newline at end of file