Skip to content

Commit c28ab9b

Browse files
authored
chore: Merge pull request #916 from DocSvartz/add-flatenning-to-interfaces
feat: add Flattening mapping for all source type
2 parents 98962ae + fe82ce0 commit c28ab9b

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/Mapster.Tests/WhenFlattening.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
using System;
2-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
32
using Shouldly;
3+
using System;
4+
using System.Collections.Generic;
45
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
56

67
namespace Mapster.Tests
@@ -78,12 +79,40 @@ public class ModelDto
7879
public string SubSubSubCoolProperty { get; set; }
7980
}
8081

82+
public class Source915
83+
{
84+
public ICollection<Case> Cases { get; set; } = new List<Case>
85+
{
86+
new Case(),
87+
new Case(),
88+
new Case()
89+
};
90+
}
91+
92+
public class Destination915
93+
{
94+
public int CasesCount { get; set; }
95+
}
8196

8297
#endregion
8398

99+
84100
[TestClass]
85101
public class WhenFlattening
86102
{
103+
104+
/// <summary>
105+
/// https://github.com/MapsterMapper/Mapster/issues/915
106+
/// </summary>
107+
[TestMethod]
108+
public void FlatteningUsingSourceInterface()
109+
{
110+
var source = new Source915();
111+
var result = source.Adapt<Destination915>();
112+
113+
result.CasesCount.ShouldBe(source.Cases.Count);
114+
}
115+
87116
[TestMethod]
88117
public void GetMethodTest()
89118
{

src/Mapster/Settings/ValueAccessingStrategy.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ public static class ValueAccessingStrategy
116116
return member.GetExpression(source);
117117

118118
var propertyType = member.Type;
119-
if (propertyName.StartsWith(sourceMemberName) &&
120-
(propertyType.IsPoco() || propertyType.IsRecordType()))
119+
if (propertyName.StartsWith(sourceMemberName) && !propertyType.IsMapsterPrimitive())
121120
{
122121
var exp = member.GetExpression(source);
123122
var ifTrue = GetDeepFlattening(exp, propertyName.Substring(sourceMemberName.Length).TrimStart('_'), arg);

0 commit comments

Comments
 (0)