From 8d61e4324cc5c39f5768ca8265e4fe5d13c53c2e Mon Sep 17 00:00:00 2001 From: wuyangfan Date: Mon, 25 May 2026 14:01:50 +0800 Subject: [PATCH 1/2] test: ensure Compile skips open generic mapping rules (#925) Add regression coverage for configuring ClassA<> to ClassB<> and calling Compile() without throwing, matching the documented validation behavior. Co-authored-by: Cursor --- .../WhenMappingWithOpenGenerics.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs b/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs index 7abb5e1f..118f1c75 100644 --- a/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs +++ b/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs @@ -33,6 +33,23 @@ public void Setting_From_OpenGeneric_Has_No_SideEffect() var cCopy = c.Adapt(config); } + /// + /// https://github.com/MapsterMapper/Mapster/issues/925 + /// + [TestMethod] + public void Compile_With_Open_Generic_Mapping_Does_Not_Throw() + { + var config = new TypeAdapterConfig(); + config.ForType(typeof(ClassA<>), typeof(ClassB<>)); + + Should.NotThrow(() => config.Compile()); + + var classA = new ClassA { Variable = 15 }; + var classB = classA.Adapt>(config); + + classB.Variable.ShouldBe(15); + } + [TestMethod] public void MapOpenGenericsUseInherits() { @@ -102,5 +119,15 @@ class A { public string AProperty { get; set; } } class B { public string BProperty { get; set; } } class C { public string BProperty { get; set; } } + + class ClassA + { + public T? Variable { get; set; } + } + + class ClassB + { + public T? Variable { get; set; } + } } } From 2c37aa1cfd9e8c3a4ea20e491d91f85f86d72ece Mon Sep 17 00:00:00 2001 From: DocSvartz Date: Mon, 15 Jun 2026 13:24:35 +0500 Subject: [PATCH 2/2] fix(test): Drop OpenGeneric compile check because a separate test was added --- src/Mapster.Tests/WhenMappingWithOpenGenerics.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs b/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs index 118f1c75..19891357 100644 --- a/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs +++ b/src/Mapster.Tests/WhenMappingWithOpenGenerics.cs @@ -24,9 +24,7 @@ public void Setting_From_OpenGeneric_Has_No_SideEffect() config .NewConfig(typeof(A<>), typeof(B<>)) .Map("BProperty", "AProperty"); - - config.Compile(); // is not throw exception - + var a = new A { AProperty = "A" }; var c = new C { BProperty = "C" }; var b = a.Adapt>(config); // successful mapping