Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Mapster.Tests/WhenConfiguringMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ public void NewInstanceConfigurationTest()
obj.Name = "Tim";
obj.Child = new TestNewInstanceF() { Name = "Kıvanç" };

TypeAdapterConfig<TestNewInstanceD, TestNewInstanceE>
TypeAdapterConfig<TestNewInstanceD, TestNewInstanceE>
.NewConfig()
.ShallowCopyForSameType(true);
.ShallowCopyForSameType(true);

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

Expand Down
7 changes: 5 additions & 2 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,13 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp
if (_source.Type == destinationType && arg.MapType == MapType.Projection)
return _source;

TypeAdapterRule? rule;
arg.Context.Config.RuleMap.TryGetValue(new TypeTuple(_source.Type, destinationType), out rule);

//adapt(_source);
var notUsingDestinationValue = mapping is not { UseDestinationValue: true };
var exp = _source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true && notUsingDestinationValue &&
!arg.Context.Config.HasRuleFor(_source.Type, destinationType)
var exp = _source.Type == destinationType && notUsingDestinationValue
&& arg.IsShallowCopyForSameTypeEnable(rule) && arg.IsNotCustomConverterFactory(rule)
? _source
: CreateAdaptExpressionCore(_source, destinationType, arg, mapping, destination);

Expand Down
25 changes: 25 additions & 0 deletions src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,5 +466,30 @@ public static bool IsNotSelfCreation(this Type type)

return type.GetFieldsAndProperties().All(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0);
}

public static bool IsShallowCopyForSameTypeEnable(this CompileArgument arg, TypeAdapterRule? rule)
{
if (rule != null)
return (rule?.Settings.ShallowCopyForSameType).GetValueOrDefault();

if (arg.Settings.ShallowCopyForSameType.GetValueOrDefault()
|| arg.Context.Config.Default.Settings.ShallowCopyForSameType.GetValueOrDefault())
return true;

return false;
}

public static bool IsNotCustomConverterFactory(this CompileArgument arg, TypeAdapterRule? rule)
{
if(rule != null)
{
if(arg.MapType == MapType.Map && rule.Settings.ConverterFactory != null)
return false;
if (arg.MapType == MapType.MapToTarget && rule.Settings.ConverterToTargetFactory != null)
return false;
}

return true;
}
}
}
Loading