Skip to content

Commit fc403e1

Browse files
authored
chore: Merge pull request #957 from DocSvartz/fix-954
fix: #954 fix mapping optional ValueType params
2 parents f1fd9f7 + 8831a38 commit fc403e1

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/Mapster.Tests/WhenCtorNullableParamMapping.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Shouldly;
3+
using System;
34
using System.Collections.Generic;
45

56
namespace Mapster.Tests
@@ -82,6 +83,27 @@ public void NullableCtorPropagationCurrentWorkWithDestinationTransform()
8283
foo.Strings.ShouldNotBeNull();
8384
}
8485

86+
87+
/// <summary>
88+
/// https://github.com/MapsterMapper/Mapster/issues/954
89+
/// </summary>
90+
[TestMethod]
91+
public void MappingValueTypeParametrUsingDefaultValueCorrect()
92+
{
93+
// Arrange
94+
var src = new DateTimeFoo954(DateTime.Today);
95+
96+
// Assert
97+
Should.NotThrow(() =>
98+
{
99+
var foo = src.Adapt<DateTimeFooDto954>();
100+
101+
foo.Timestamp.ShouldBe(src.Timestamp);
102+
});
103+
}
104+
105+
106+
85107
#region Immutable classes with private setters, map via ctors
86108
private abstract class AbstractDomainTestClass
87109
{
@@ -119,6 +141,18 @@ public DomainTestClass(
119141

120142
#region DTO classes
121143

144+
public class DateTimeFooDto954
145+
{
146+
public DateTime Timestamp { get; set; }
147+
148+
public DateTimeFooDto954(DateTime timestamp = default(DateTime))
149+
{
150+
this.Timestamp = timestamp;
151+
}
152+
}
153+
154+
record DateTimeFoo954(DateTime Timestamp);
155+
122156
class FooDto943
123157
{
124158
public string[] Strings { get; set; }

src/Mapster/Adapters/BaseClassAdapter.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,27 @@ protected Expression CreateInstantiationExpression(Expression source, ClassMappi
222222
{
223223
arg.Context.NullChecks.UnionWith(members.Where(x=>x.Getter != null).Select(x=>(x.Getter,arg)));
224224
var parameterInfo = (ParameterInfo)member.DestinationMember.Info!;
225-
var defaultConst = parameterInfo.IsOptional
225+
Expression defaultConst;
226+
Expression getter;
227+
228+
#if NETSTANDARD2_0
229+
try
230+
{
231+
defaultConst = parameterInfo.IsOptional && parameterInfo.DefaultValue != null
226232
? Expression.Constant(parameterInfo.DefaultValue, member.DestinationMember.Type)
227233
: parameterInfo.ParameterType.CreateDefault();
234+
}
235+
catch (FormatException)
236+
{
237+
defaultConst = parameterInfo.ParameterType.CreateDefault();
238+
}
228239

229-
Expression getter;
240+
#else
241+
defaultConst = parameterInfo.IsOptional && parameterInfo.DefaultValue != null
242+
? Expression.Constant(parameterInfo.DefaultValue, member.DestinationMember.Type)
243+
: parameterInfo.ParameterType.CreateDefault();
244+
#endif
245+
230246
if (member.Getter == null)
231247
{
232248
getter = defaultConst;
@@ -352,6 +368,6 @@ protected static Expression SetValueTypeAutoPropertyByReflection(MemberMapping m
352368
new[] { member.Destination, memberAsObject });
353369
}
354370

355-
#endregion
371+
#endregion
356372
}
357373
}

0 commit comments

Comments
 (0)