Skip to content

Commit 5467c62

Browse files
committed
#fix 242 open generic setting shouldn't have side effect
1 parent a15818d commit 5467c62

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Mapster.Tests/WhenMappingWithOpenGenerics.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ public void Map_With_Open_Generics()
2222
dto.value.ShouldBe(poco.Value);
2323
}
2424

25+
[TestMethod]
26+
public void Setting_From_OpenGeneric_Has_No_SideEffect()
27+
{
28+
var config = new TypeAdapterConfig();
29+
config
30+
.NewConfig(typeof(A<>), typeof(B<>))
31+
.Map("BProperty", "AProperty");
32+
33+
var a = new A<C> { AProperty = "A" };
34+
var c = new C { BProperty = "C" };
35+
var b = a.Adapt<B<C>>(config); // successful mapping
36+
var cCopy = c.Adapt<C>(config);
37+
}
38+
2539
public class GenericPoco<T>
2640
{
2741
public T Value { get; set; }
@@ -31,6 +45,11 @@ public class GenericDto<T>
3145
{
3246
public T value { get; set; }
3347
}
34-
48+
49+
class A<T> { public string AProperty { get; set; } }
50+
51+
class B<T> { public string BProperty { get; set; } }
52+
53+
class C { public string BProperty { get; set; } }
3554
}
3655
}

src/Mapster/Mapster.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.1</NetStandardImplicitPackageVersion>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
2121
<RootNamespace>Mapster</RootNamespace>
22-
<Version>5.3.1</Version>
22+
<Version>5.3.2</Version>
2323
<LangVersion>8.0</LangVersion>
2424
<Nullable>enable</Nullable>
2525
<NoWarn>1701;1702;8618</NoWarn>

src/Mapster/TypeAdapterConfig.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ private static TypeAdapterRule CreateDestinationTypeRule(TypeTuple key)
219219
score--;
220220
type1 = type1.GetTypeInfo().BaseType;
221221
}
222-
return type1 == null ? null : (int?)score;
222+
return type1 != null && type1.GetTypeInfo().IsGenericType && type1.GetGenericTypeDefinition() == type2
223+
? (int?)score
224+
: null;
223225
}
224226
if (!allowInheritance)
225227
return null;
@@ -302,8 +304,7 @@ internal MethodCallExpression GetProjectionCallExpression(Type sourceType, Type
302304
private Dictionary<TypeTuple, Delegate>? _dynamicMapDict;
303305
internal Func<object, TDestination> GetDynamicMapFunction<TDestination>(Type sourceType)
304306
{
305-
if (_dynamicMapDict == null)
306-
_dynamicMapDict = new Dictionary<TypeTuple, Delegate>();
307+
_dynamicMapDict ??= new Dictionary<TypeTuple, Delegate>();
307308
var key = new TypeTuple(sourceType, typeof(TDestination));
308309
if (!_dynamicMapDict.TryGetValue(key, out var del))
309310
del = AddToHash(_dynamicMapDict, key, tuple => Compiler(CreateDynamicMapExpression(tuple)));
@@ -618,8 +619,7 @@ public TypeAdapterConfig Fork(Action<TypeAdapterConfig> action,
618619
#endif
619620
int key2 = 0)
620621
{
621-
if (_inlineConfigs == null)
622-
_inlineConfigs = new Dictionary<string, TypeAdapterConfig>();
622+
_inlineConfigs ??= new Dictionary<string, TypeAdapterConfig>();
623623
var key = key1 + '|' + key2;
624624
if (_inlineConfigs.TryGetValue(key, out var config))
625625
return config;

0 commit comments

Comments
 (0)