-
Notifications
You must be signed in to change notification settings - Fork 407
fix: honor per-type ShallowCopyForSameType overrides (#938) #974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| using Mapster.Models; | ||
| using MapsterMapper; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Shouldly; | ||
|
|
||
| namespace Mapster.Tests | ||
| { | ||
| /// <summary> | ||
| /// https://github.com/MapsterMapper/Mapster/issues/938 | ||
| /// </summary> | ||
| [TestClass] | ||
| public class WhenPerTypeShallowCopyOverride | ||
| { | ||
| [TestMethod] | ||
| public void GlobalShallowCopy_WithPerTypeDeepCopy_ShouldRestoreViaMapToTarget() | ||
| { | ||
| var cfg = new TypeAdapterConfig(); | ||
| cfg.RequireExplicitMapping = true; | ||
| cfg.Default.ShallowCopyForSameType(true); | ||
| cfg.Default.AvoidInlineMapping(true); | ||
|
|
||
| cfg.NewConfig<RandomObject2, RandomObject2>(); | ||
| cfg.NewConfig<RandomObject1, RandomObject1>(); | ||
|
|
||
| cfg.NewConfig<MyFailStuff, MyFailStuff>() | ||
| .ShallowCopyForSameType(false); | ||
|
|
||
| cfg.GetMergedSettings(new TypeTuple(typeof(MyFailStuff), typeof(MyFailStuff)), MapType.Map) | ||
| .ShallowCopyForSameType.ShouldBe(false); | ||
|
|
||
| cfg.Compile(); | ||
|
|
||
| var mapper = new Mapper(cfg); | ||
|
|
||
| var dynamicStuff = new MyFailStuff(); | ||
| dynamicStuff.Item1 = new RandomObject1(); | ||
| dynamicStuff.Item1.SampleName = "SN1"; | ||
| dynamicStuff.Item2 = new RandomObject2(); | ||
| dynamicStuff.Item2.SampleNumber = 2; | ||
|
|
||
| var originalStuff = mapper.Map<MyFailStuff, MyFailStuff>(dynamicStuff); | ||
|
|
||
| dynamicStuff.Item1.SampleName = "SN1CHANGED"; | ||
| dynamicStuff.Item2.SampleNumber = 3; | ||
|
|
||
| mapper.Map(originalStuff, dynamicStuff); | ||
|
|
||
| dynamicStuff.Item1.SampleName.ShouldBe("SN1"); | ||
| dynamicStuff.Item2.SampleNumber.ShouldBe(2); | ||
| ReferenceEquals(dynamicStuff.Item1, originalStuff.Item1).ShouldBeFalse(); | ||
| ReferenceEquals(dynamicStuff.Item2, originalStuff.Item2).ShouldBeFalse(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void ParentDeepCopyOverride_ShouldNotDisableImplicitNestedShallowCopyForSameType() | ||
| { | ||
| var cfg = new TypeAdapterConfig(); | ||
| cfg.Default.ShallowCopyForSameType(true); | ||
| cfg.NewConfig<Container, Container>() | ||
| .ShallowCopyForSameType(false); | ||
|
|
||
| cfg.Compile(); | ||
|
|
||
| var src = new Container { Child = new NestedChild { Value = 1 } }; | ||
| var dest = src.Adapt<Container>(cfg); | ||
|
|
||
| ReferenceEquals(src.Child, dest.Child).ShouldBeTrue(); | ||
| } | ||
|
|
||
| public class Container | ||
| { | ||
| public NestedChild? Child { get; set; } | ||
| } | ||
|
|
||
| public class NestedChild | ||
| { | ||
| public int Value { get; set; } | ||
| } | ||
|
|
||
| public class MyFailStuff | ||
| { | ||
| public MyFailStuff() | ||
| { | ||
| CreateEmptyEntities(); | ||
| } | ||
|
|
||
| public void CreateEmptyEntities() | ||
| { | ||
| Item1 ??= new RandomObject1(); | ||
| Item2 ??= new RandomObject2(); | ||
| } | ||
|
|
||
| public RandomObject1? Item1 { get; set; } | ||
|
|
||
| public RandomObject2? Item2 { get; set; } | ||
| } | ||
|
|
||
| public class RandomObject1 | ||
| { | ||
| public string? SampleName { get; set; } | ||
| } | ||
|
|
||
| public class RandomObject2 | ||
| { | ||
| public int SampleNumber { get; set; } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,8 +503,12 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp | |
| var notUsingDestinationValue = mapping is not { UseDestinationValue: true }; | ||
| Expression exp; | ||
|
|
||
| if (_source.Type == destinationType && arg.Settings.ShallowCopyForSameType == true | ||
| && notUsingDestinationValue && rule == null) | ||
| var shallowCopySettings = _source.Type == destinationType | ||
| ? arg.Context.Config.GetMergedSettings(tuple, arg.MapType) | ||
| : arg.Settings; | ||
|
|
||
| if (_source.Type == destinationType && shallowCopySettings.ShallowCopyForSameType == true | ||
| && notUsingDestinationValue) | ||
| exp = _source; | ||
|
Comment on lines
+515
to
517
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the wrong level of mapping.
If you consider this a new TopLevel, a new object must be created here in any case, and its properties must be copied by reference. |
||
| else if (source is ConditionalExpression cond && mapping != null) | ||
| { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is not valid and fail ❌
To obtain the desired result, the issue author used an incorrect configuration.
This is what the author wanted to achieve.