Skip to content

Commit

Permalink
Merge pull request #601 from rafalka/bugfix/600-fixing-ShallowCopyFor…
Browse files Browse the repository at this point in the history
…SameType-UseDestinationValue-UseDestinationValue-interactions

Fixing incorrect mapping generation when ShallowCopyForSameType, UseDestinationValue and UseDestinationValue are used
  • Loading branch information
andrerav authored Jun 27, 2023
2 parents cc81763 + 70a98d5 commit 9feaad3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Mapster.Tests/WhenUsingDestinationValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ public class WhenUsingDestinationValue
public void MapUsingDestinationValue()
{
TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo();
TypeAdapterConfig.GlobalSettings.Default.ShallowCopyForSameType(true);
TypeAdapterConfig<Invoice, InvoiceDto>.NewConfig().TwoWays();

var strings = new[] { "One, Two, Three" };
var dto = new InvoiceDto
{
Id = 1,
DocumentNumber = "AA001",
SupplierCompany = "COM01",
SupplierName = "Apple",
Numbers = Enumerable.Range(1, 5).ToList(),
Strings = strings,
};
var poco = dto.Adapt<Invoice>();
poco.Id.ShouldBe(dto.Id);
poco.DocumentNumber.ShouldBe("FOO");
poco.Supplier.Name.ShouldBe(dto.SupplierName);
poco.Supplier.Company.ShouldBe(dto.SupplierCompany);
poco.Numbers.ShouldBe(Enumerable.Range(1, 5));
poco.Strings.ShouldBe(strings);
}

public class ContractingParty
Expand All @@ -49,6 +53,9 @@ public class Invoice

[UseDestinationValue]
public ICollection<int> Numbers { get; } = new List<int>();

[UseDestinationValue]
public ICollection<string> Strings { get; } = new List<string>();
}

public class InvoiceDto
Expand All @@ -58,6 +65,7 @@ public class InvoiceDto
public string SupplierName { get; set; }
public string SupplierCompany { get; set; }
public IEnumerable<int> Numbers { get; set; }
public ICollection<string> Strings { get; set; }
}
}
}
18 changes: 12 additions & 6 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,23 @@ protected Expression CreateAdaptExpression(Expression source, Type destinationTy
}
internal Expression CreateAdaptExpression(Expression source, Type destinationType, CompileArgument arg, MemberMapping? mapping, Expression? destination = null)
{
if (source.Type == destinationType &&
(arg.Settings.ShallowCopyForSameType == true || arg.MapType == MapType.Projection))
if (source.Type == destinationType && arg.MapType == MapType.Projection)
return source;

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

//transform(adapt(source));
var transform = arg.Settings.DestinationTransforms.Find(it => it.Condition(exp.Type));
if (transform != null)
exp = transform.TransformFunc(exp.Type).Apply(arg.MapType, exp);
if (notUsingDestinationValue)
{
var transform = arg.Settings.DestinationTransforms.Find(it => it.Condition(exp.Type));
if (transform != null)
exp = transform.TransformFunc(exp.Type).Apply(arg.MapType, exp);
}

return exp.To(destinationType);
}
}
Expand Down

0 comments on commit 9feaad3

Please sign in to comment.