Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/Mapster.Tests/WhenMappingDerived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,82 @@ public void WhenMappingDerivedWithoutMembers()
Assert.AreEqual(inputEntity.Id, result.Id);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/794
/// </summary>
[TestMethod]
public void WhenMapToTargetDerivedWithNullRegression()
{
var config = new TypeAdapterConfig();

config
.NewConfig<ContainerDTO794, Container794>()
.Map(dest => dest.Nested, src => src.NestedDTO)
.IgnoreNonMapped(true)
.IgnoreNullValues(true);
config
.NewConfig<BaseDTO794, Base794>()
.Map(dest => dest.SomeBaseProperty, src => src.SomeBasePropertyDTO)
.Include<DerivedDTO794, Base794>()
.IgnoreNonMapped(true)
.IgnoreNullValues(true);

config
.NewConfig<DerivedDTO794, Derived794>()
.Map(dest => dest.SomeDerivedProperty, src => src.SomeDerivedPropertyDTO)
.IgnoreNonMapped(true)
.IgnoreNullValues(true);
config
.NewConfig<DerivedDTO794, Base794>()
.MapWith(src => src.Adapt<Derived794E>());


var container = new Container794();
var containerDTO = new ContainerDTO794();

container.Nested = null;
containerDTO.NestedDTO = new DerivedDTO794();

containerDTO.Adapt<ContainerDTO794, Container794>(container, config);

(container.Nested is Derived794E).ShouldBeTrue(); // is not Base794 type, MapWith is working when Polymorphic mapping to null
}

internal class Derived794E : Derived794
{

}

internal class Base794
{
public string SomeBaseProperty { get; set; }
}

internal class BaseDTO794
{
public string SomeBasePropertyDTO { get; set; }
}

internal class Derived794 : Base794
{
public string SomeDerivedProperty { get; set; }
}

internal class DerivedDTO794 : BaseDTO794
{
public string SomeDerivedPropertyDTO { get; set; }
}

internal class Container794
{
public Base794 Nested { get; set; }
}

internal class ContainerDTO794
{
public BaseDTO794 NestedDTO { get; set; }
}

internal class BaseDto
{
public long Id { get; set; }
Expand Down
9 changes: 6 additions & 3 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
drvdDest,
Expression.TypeAs(destination, tuple.Destination));
blocks.Add(drvdDestAssign);
cond = Expression.AndAlso(
cond,
Expression.NotEqual(drvdDest, Expression.Constant(null, tuple.Destination)));

// fix by https://github.com/MapsterMapper/Mapster/issues/794
// This can be removed if it does not cause any other bugs.
// cond = Expression.AndAlso(
// cond,
// Expression.NotEqual(drvdDest, Expression.Constant(null, tuple.Destination)));
}

var adaptExpr = CreateAdaptExpressionCore(drvdSource, tuple.Destination, arg, destination: drvdDest);
Expand Down
Loading