Skip to content

Commit

Permalink
Merge pull request #629 from rafalka/bugfix/628-custom-coversion-igno…
Browse files Browse the repository at this point in the history
…red-when-ShallowCopyForSameType-is-set

Not using shallow copy if mapping rule exists
  • Loading branch information
andrerav authored Sep 21, 2023
2 parents c49bc32 + a984fc7 commit c337c95
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/Mapster.Tests/WhenSettingMapToTargetWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,40 @@ public void MapToTargetWith_Should_Work_With_Adapt_To_Target()
var a = new Foo { A = 1 };
var b = new Bar { A = 2 };

//This will not work as expected => b.A will be 1, ignoring the mapping defined
var config = new TypeAdapterConfig();
config.Default.ShallowCopyForSameType(true);

config.NewConfig<double, double>().MapToTargetWith((x, y) => 5);
a.Adapt(b, config);
b.A.ShouldBe(5);
}

[TestMethod]
public void MapWith_Should_Work_With_Adapt_To_Target()
{
var a = new List<double> {1, 2, 3};

var config = new TypeAdapterConfig();
config.Default.ShallowCopyForSameType(true);

config.NewConfig<double, double>().MapWith(_ => 5);
var b = a.Adapt<List<double>>(config);
b.ShouldBe(new List<double>{ 5, 5, 5});
}

[TestMethod]
public void MapWith_Should_Work_With_Adapt_To_Target_For_Collection()
{
var a = new List<double> {1, 2, 3};

var config = new TypeAdapterConfig();
config.Default.ShallowCopyForSameType(true);

config.NewConfig<List<double>, List<double>>().MapWith(_ => new List<double>{ 5, 5, 5});
var b = a.Adapt<List<double>>(config);
b.ShouldBe(new List<double>{ 5, 5, 5});
}

internal class Foo
{
public double A { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp

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

Expand Down
4 changes: 4 additions & 0 deletions src/Mapster/Utils/TypeAdapterConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Mapster.Models;

namespace Mapster.Utils;

Expand All @@ -27,4 +28,7 @@ internal static void ScanInheritedTypes(this TypeAdapterConfig config, List<Type
InterfaceDynamicMapper dynamicMapper = new(config, types);
dynamicMapper.ApplyMappingFromAssembly();
}

public static bool HasRuleFor(this TypeAdapterConfig config, Type srcType, Type dstType) =>
config.RuleMap.ContainsKey(new TypeTuple(srcType, dstType));
}

0 comments on commit c337c95

Please sign in to comment.