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
34 changes: 34 additions & 0 deletions src/Mapster.Tests/WhenCtorNullableParamMapping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;

namespace Mapster.Tests
Expand Down Expand Up @@ -82,6 +83,27 @@ public void NullableCtorPropagationCurrentWorkWithDestinationTransform()
foo.Strings.ShouldNotBeNull();
}


/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/954
/// </summary>
[TestMethod]
public void MappingValueTypeParametrUsingDefaultValueCorrect()
{
// Arrange
var src = new DateTimeFoo954(DateTime.Today);

// Assert
Should.NotThrow(() =>
{
var foo = src.Adapt<DateTimeFooDto954>();

foo.Timestamp.ShouldBe(src.Timestamp);
});
}



#region Immutable classes with private setters, map via ctors
private abstract class AbstractDomainTestClass
{
Expand Down Expand Up @@ -119,6 +141,18 @@ public DomainTestClass(

#region DTO classes

public class DateTimeFooDto954
{
public DateTime Timestamp { get; set; }

public DateTimeFooDto954(DateTime timestamp = default(DateTime))
{
this.Timestamp = timestamp;
}
}

record DateTimeFoo954(DateTime Timestamp);

class FooDto943
{
public string[] Strings { get; set; }
Expand Down
22 changes: 19 additions & 3 deletions src/Mapster/Adapters/BaseClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#region Build the Adapter Model

protected ClassMapping CreateClassConverter(Expression source, ClassModel classModel, CompileArgument arg, Expression? destination = null, bool ctorMapping = false, ClassModel recordRestorMemberModel = null)

Check warning on line 18 in src/Mapster/Adapters/BaseClassAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var destinationMembers = classModel.Members;
var unmappedDestinationMembers = new List<string>();
Expand Down Expand Up @@ -213,7 +213,7 @@
&& ignore.Condition == null;
}

protected Expression CreateInstantiationExpression(Expression source, ClassMapping classConverter, CompileArgument arg, Expression? destination, ClassModel recordRestorParamModel = null)

Check warning on line 216 in src/Mapster/Adapters/BaseClassAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var members = classConverter.Members;

Expand All @@ -222,11 +222,27 @@
{
arg.Context.NullChecks.UnionWith(members.Where(x=>x.Getter != null).Select(x=>(x.Getter,arg)));
var parameterInfo = (ParameterInfo)member.DestinationMember.Info!;
var defaultConst = parameterInfo.IsOptional
Expression defaultConst;
Expression getter;

#if NETSTANDARD2_0
try
{
defaultConst = parameterInfo.IsOptional && parameterInfo.DefaultValue != null
? Expression.Constant(parameterInfo.DefaultValue, member.DestinationMember.Type)
: parameterInfo.ParameterType.CreateDefault();
}
catch (FormatException)
{
defaultConst = parameterInfo.ParameterType.CreateDefault();
}

Expression getter;
#else
defaultConst = parameterInfo.IsOptional && parameterInfo.DefaultValue != null
? Expression.Constant(parameterInfo.DefaultValue, member.DestinationMember.Type)
: parameterInfo.ParameterType.CreateDefault();
#endif

if (member.Getter == null)
{
getter = defaultConst;
Expand Down Expand Up @@ -352,6 +368,6 @@
new[] { member.Destination, memberAsObject });
}

#endregion
#endregion
}
}
Loading