Skip to content

Commit b4feddb

Browse files
authored
chore: Merge pull request #885 from DocSvartz/add-class-ctor--using-default-value-for-param
Fix #883 - Add class ctor using default value for param
2 parents ffa4633 + a60020b commit b4feddb

3 files changed

Lines changed: 48 additions & 18 deletions

File tree

src/Mapster.Tests/WhenMappingRecordRegression.cs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Shouldly;
33
using System;
44
using System.Collections.Generic;
5+
using static Mapster.Tests.WhenExplicitMappingRequired;
56
using static Mapster.Tests.WhenMappingDerived;
67

78
namespace Mapster.Tests
@@ -474,22 +475,6 @@ public void ClassCtorAutomapingWorking()
474475
result.X.ShouldBe(100);
475476
}
476477

477-
/// <summary>
478-
/// https://github.com/MapsterMapper/Mapster/issues/842
479-
/// </summary>
480-
[TestMethod]
481-
public void ClassCustomCtorWitoutMapNotWorking()
482-
{
483-
TypeAdapterConfig.GlobalSettings.Clear();
484-
485-
var source = new TestRecord() { X = 100 };
486-
487-
Should.Throw<InvalidOperationException>(() =>
488-
{
489-
source.Adapt<AutoCtorDestYx>();
490-
});
491-
}
492-
493478
/// <summary>
494479
/// https://github.com/MapsterMapper/Mapster/issues/842
495480
/// </summary>
@@ -537,6 +522,24 @@ public void ClassUpdateAutoPropertyWitoutSetterWorking()
537522
result.X.ShouldBe(200);
538523
}
539524

525+
/// <summary>
526+
/// https://github.com/MapsterMapper/Mapster/issues/883
527+
/// </summary>
528+
[TestMethod]
529+
public void ClassCtorActivateDefaultValue()
530+
{
531+
var source = new Source833
532+
{
533+
Value1 = "123",
534+
};
535+
536+
Should.NotThrow(() =>
537+
{
538+
var target = source.Adapt<Target833>();
539+
target.Value1.ShouldBe("123");
540+
target.Value2.ShouldBe(default);
541+
});
542+
}
540543

541544
#region NowNotWorking
542545

@@ -565,6 +568,24 @@ public void CollectionUpdate()
565568

566569
#region TestClasses
567570

571+
public class Source833
572+
{
573+
public required string Value1 { get; init; }
574+
}
575+
576+
public class Target833
577+
{
578+
public Target833(string value1, string value2)
579+
{
580+
Value1 = value1;
581+
Value2 = value2;
582+
}
583+
584+
public string Value1 { get; }
585+
586+
public string Value2 { get; }
587+
}
588+
568589
public sealed record Database746(
569590
string Server = "",
570591
string Name = "",

src/Mapster.Tests/WhenUsingNonDefaultConstructor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public void Map_To_Existing_Destination_Instance_Should_Pass()
7272
dto.Unmapped.ShouldBe("unmapped");
7373
}
7474

75+
/// <summary>
76+
/// ignore after implement fix https://github.com/MapsterMapper/Mapster/issues/883
77+
/// </summary>
78+
[Ignore]
7579
[TestMethod]
7680
public void Map_To_Destination_Type_Without_Default_Constructor_Shoud_Throw_Exception()
7781
{

src/Mapster/Adapters/ClassAdapter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ protected override Expression CreateInstantiationExpression(Expression source, E
6666
: arg.DestinationType;
6767
if (destType == null)
6868
return base.CreateInstantiationExpression(source, destination, arg);
69-
classConverter = destType.GetConstructors()
69+
70+
var constructors = destType.GetConstructors();
71+
classConverter = constructors
7072
.OrderByDescending(it => it.GetParameters().Length)
7173
.Select(it => GetConstructorModel(it, true))
72-
.Select(it => CreateClassConverter(source, it, arg, ctorMapping:true))
74+
.Select(it => CreateClassConverter(source, it, arg, ctorMapping: true))
7375
.FirstOrDefault(it => it != null);
76+
77+
if (classConverter == null && constructors.Length > 0)
78+
classConverter = CreateClassConverter(source, GetConstructorModel(constructors[0], false), arg, ctorMapping: true);
7479
}
7580
else
7681
{

0 commit comments

Comments
 (0)