Skip to content

Commit 39a4ca4

Browse files
authored
fix: regression when update required property (#902)
* fix(test): fix RequiredProperty test * fix: Regression in v10 - Forced Update of Required Property #899
1 parent e192f41 commit 39a4ca4

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

src/Mapster.Core/Enums/MapType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public enum MapType
88
Map = 1,
99
MapToTarget = 2,
1010
Projection = 4,
11+
ApplyNullPropagation = 8,
1112
}
1213
}

src/Mapster.Tests/WhenMappingRecordRegression.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,7 @@ public void RequiredProperty()
449449
{
450450
var source = new Person553 { FirstMidName = "John", LastName = "Dow" };
451451
var destination = new Person554 { ID = 245, FirstMidName = "Mary", LastName = "Dow" };
452-
453-
TypeAdapterConfig<Person553, Person554>.NewConfig()
454-
//.Map(dest => dest.ID, source => 0)
455-
.Ignore(x => x.ID);
456-
452+
457453
var s = source.BuildAdapter().CreateMapToTargetExpression<Person554>();
458454

459455
var result = source.Adapt(destination);

src/Mapster/Adapters/BaseAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
220220
.Any(y => y.GetType().FullName == "System.Runtime.CompilerServices.RequiredMemberAttribute"));
221221

222222
if (requiremembers.Count() != 0)
223-
set = CreateInlineExpression(source, arg, true);
223+
set = CreateInlineExpression(source, arg.CloneWith(MapType.ApplyNullPropagation), true);
224224
else
225225
set = CreateInstantiationExpression(transformedSource, destination, arg);
226226

src/Mapster/Adapters/BaseClassAdapter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ select fn(src, destinationMember, arg))
113113
Destination = (ParameterExpression?)destination,
114114
UseDestinationValue = IsCanUsingDestinationValue(arg, destinationMember),
115115
};
116-
if(getter == null && !arg.DestinationType.IsRecordType()
116+
if(arg.MapType == MapType.ApplyNullPropagation &&
117+
getter == null && !arg.DestinationType.IsRecordType()
117118
&& destinationMember.Info is PropertyInfo propinfo)
118119
{
119120
if (propinfo.GetCustomAttributes()

src/Mapster/Compile/CompileArgument.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,29 @@ select split
4848
_fetchConstructUsing = true;
4949
return _constructUsing;
5050
}
51+
52+
public CompileArgument CloneWith(MapType? mapType = null)
53+
{
54+
var result = new CompileArgument()
55+
{
56+
SourceType = this.SourceType,
57+
DestinationType = this.DestinationType,
58+
MapType = this.MapType,
59+
ExplicitMapping = this.ExplicitMapping,
60+
Settings = this.Settings,
61+
Context = this.Context,
62+
UseDestinationValue = this.UseDestinationValue,
63+
ConstructorMapping = this.ConstructorMapping,
64+
_srcNames = this._srcNames,
65+
_destNames = this._destNames,
66+
_fetchConstructUsing = this._fetchConstructUsing,
67+
_constructUsing = this._constructUsing
68+
};
69+
70+
if (mapType != null)
71+
result.MapType = mapType.Value;
72+
73+
return result;
74+
}
5175
}
5276
}

0 commit comments

Comments
 (0)