Skip to content

Commit d28a0c6

Browse files
committed
fix: #938 - ShallowCopyForSame work if custom mapping rule register
1 parent a0cd3c1 commit d28a0c6

3 files changed

Lines changed: 32 additions & 4 deletions

File tree

src/Mapster.Tests/WhenConfiguringMapping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ public void NewInstanceConfigurationTest()
142142
obj.Name = "Tim";
143143
obj.Child = new TestNewInstanceF() { Name = "Kıvanç" };
144144

145-
TypeAdapterConfig<TestNewInstanceD, TestNewInstanceE>
145+
TypeAdapterConfig<TestNewInstanceD, TestNewInstanceE>
146146
.NewConfig()
147-
.ShallowCopyForSameType(true);
147+
.ShallowCopyForSameType(true);
148148

149149
var newObj2 = TypeAdapter.Adapt<TestNewInstanceD, TestNewInstanceE>(obj);
150150

src/Mapster/Adapters/BaseAdapter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,13 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp
495495
if (_source.Type == destinationType && arg.MapType == MapType.Projection)
496496
return _source;
497497

498+
TypeAdapterRule? rule;
499+
arg.Context.Config.RuleMap.TryGetValue(new TypeTuple(_source.Type, destinationType), out rule);
500+
498501
//adapt(_source);
499502
var notUsingDestinationValue = mapping is not { UseDestinationValue: true };
500-
var exp = _source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true && notUsingDestinationValue &&
501-
!arg.Context.Config.HasRuleFor(_source.Type, destinationType)
503+
var exp = _source.Type == destinationType && notUsingDestinationValue
504+
&& arg.IsShallowCopyForSameTypeEnable(rule) && arg.IsNotCustomConverterFactory(rule)
502505
? _source
503506
: CreateAdaptExpressionCore(_source, destinationType, arg, mapping, destination);
504507

src/Mapster/Utils/ReflectionUtils.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,5 +466,30 @@ public static bool IsNotSelfCreation(this Type type)
466466

467467
return type.GetFieldsAndProperties().All(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0);
468468
}
469+
470+
public static bool IsShallowCopyForSameTypeEnable(this CompileArgument arg, TypeAdapterRule? rule)
471+
{
472+
if (rule != null)
473+
return (rule?.Settings.ShallowCopyForSameType).GetValueOrDefault();
474+
475+
if (arg.Settings.ShallowCopyForSameType.GetValueOrDefault()
476+
|| arg.Context.Config.Default.Settings.ShallowCopyForSameType.GetValueOrDefault())
477+
return true;
478+
479+
return false;
480+
}
481+
482+
public static bool IsNotCustomConverterFactory(this CompileArgument arg, TypeAdapterRule? rule)
483+
{
484+
if(rule != null)
485+
{
486+
if(arg.MapType == MapType.Map && rule.Settings.ConverterFactory != null)
487+
return false;
488+
if (arg.MapType == MapType.MapToTarget && rule.Settings.ConverterToTargetFactory != null)
489+
return false;
490+
}
491+
492+
return true;
493+
}
469494
}
470495
}

0 commit comments

Comments
 (0)