Skip to content

Commit

Permalink
Fixed #510 where existing structs were incorrectly mapped.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrerav committed Jan 7, 2023
1 parent ef96339 commit 7d8a172
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
80 changes: 80 additions & 0 deletions src/Mapster.Tests/WhenMappingStructRegression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Mapster;
using MapsterMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Mapster.Tests.WhenMappingStructRegression;

namespace Mapster.Tests
{
/// <summary>
/// Regression tests for https://github.com/MapsterMapper/Mapster/issues/510
/// </summary>
[TestClass]
public class WhenMappingStructRegression
{
public struct SourceClass
{
public string Name { get; set; }
}

public struct SourceStruct
{
public string Name { get; set; }
}
public struct DestinationStruct
{
public string Name { get; set; }

public string Ignore { get; set; }
}

[TestMethod]
public void TestMapStructToExistingStruct()
{

TypeAdapterConfig<SourceStruct, DestinationStruct>
.ForType()
.Ignore(s => s.Ignore);

var source = new SourceStruct
{
Name = "Some Name",
};
var dest = new DestinationStruct
{
Ignore = "Ignored property",
};
dest = source.Adapt(dest);

dest.Ignore.ShouldBe("Ignored property");
dest.Name.ShouldBe("Some Name");
}

[TestMethod]
public void TestMapClassToExistingStruct()
{

TypeAdapterConfig<SourceClass, DestinationStruct>
.ForType()
.Ignore(s => s.Ignore);

var source = new SourceClass
{
Name = "Some Name",
};
var dest = new DestinationStruct
{
Ignore = "Ignored property",
};
dest = source.Adapt(dest);

dest.Ignore.ShouldBe("Ignored property");
dest.Name.ShouldBe("Some Name");
}
}
}
15 changes: 12 additions & 3 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,19 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
transformedSource = src;
}
var set = CreateInstantiationExpression(transformedSource, destination, arg);
if (destination != null && (this.UseTargetValue || arg.UseDestinationValue) && arg.GetConstructUsing()?.Parameters.Count != 2 && destination.CanBeNull())
if (destination != null && (this.UseTargetValue || arg.UseDestinationValue) && arg.GetConstructUsing()?.Parameters.Count != 2)
{
//dest ?? new TDest();
set = Expression.Coalesce(destination, set);
if (destination.CanBeNull())
{
//dest ?? new TDest();
set = Expression.Coalesce(destination, set);
}
else if (destination.Type.IsValueType && !destination.CanBeNull())
{
// Destination already exists, and this is a struct, so simply use the destination object
set = destination;
}

}

if (set.NodeType == ExpressionType.Throw)
Expand Down

0 comments on commit 7d8a172

Please sign in to comment.