Skip to content

Commit

Permalink
fix MapsterMapper#281 null propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Nov 22, 2020
1 parent bd353d4 commit 773f64a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Mapster.Tests/WhenFlattening.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class E
public decimal BClass_Total { get; set; }
}

public class F
{
public decimal? BClass_Total { get; set; }
}

public class ModelObject
{
public DateTime BaseDate { get; set; }
Expand Down Expand Up @@ -106,6 +111,20 @@ public void PropertyTest_NameWithUnderscore()
Assert.IsTrue(e.BClass_Total == 250);
}

[TestMethod]
public void PropertyTest_NullPropagation()
{
var f = TypeAdapter.Adapt<C, F>(new C { BClass = new B { Total = 250 } });

Assert.IsNotNull(f);
Assert.IsTrue(f.BClass_Total == 250);

var f2 = TypeAdapter.Adapt<C, F>(new C { BClass = null });

Assert.IsNotNull(f2);
Assert.IsNull(f2.BClass_Total);
}

[TestMethod]
public void ShouldUseNestedObjectPropertyMembers()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Mapster/Utils/ExpressionEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ public static Expression ApplyNullPropagation(this Expression getter)
if (expr.CanBeNull())
{
var compareNull = Expression.Equal(expr, Expression.Constant(null, expr.Type));
if (!result.Type.CanBeNull())
result = Expression.Convert(result, typeof(Nullable<>).MakeGenericType(result.Type));
result = Expression.Condition(compareNull, result.Type.CreateDefault(), result);
}

Expand Down

0 comments on commit 773f64a

Please sign in to comment.