From 8d61e4324cc5c39f5768ca8265e4fe5d13c53c2e Mon Sep 17 00:00:00 2001 From: wuyangfan Date: Mon, 25 May 2026 14:01:50 +0800 Subject: [PATCH] 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; } + } } }