Skip to content
Open
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
53 changes: 37 additions & 16 deletions src/Mapster.Tests/WhenMappingRecordRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Shouldly;
using System;
using System.Collections.Generic;
using static Mapster.Tests.WhenExplicitMappingRequired;
using static Mapster.Tests.WhenMappingDerived;

namespace Mapster.Tests
Expand Down Expand Up @@ -474,22 +475,6 @@ public void ClassCtorAutomapingWorking()
result.X.ShouldBe(100);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/842
/// </summary>
[TestMethod]
public void ClassCustomCtorWitoutMapNotWorking()
{
TypeAdapterConfig.GlobalSettings.Clear();

var source = new TestRecord() { X = 100 };

Should.Throw<InvalidOperationException>(() =>
{
source.Adapt<AutoCtorDestYx>();
});
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/842
/// </summary>
Expand Down Expand Up @@ -537,6 +522,24 @@ public void ClassUpdateAutoPropertyWitoutSetterWorking()
result.X.ShouldBe(200);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/883
/// </summary>
[TestMethod]
public void ClassCtorActivateDefaultValue()
{
var source = new Source833
{
Value1 = "123",
};

Should.NotThrow(() =>
{
var target = source.Adapt<Target833>();
target.Value1.ShouldBe("123");
target.Value2.ShouldBe(default);
});
}

#region NowNotWorking

Expand Down Expand Up @@ -565,6 +568,24 @@ public void CollectionUpdate()

#region TestClasses

public class Source833
{
public required string Value1 { get; init; }
}

public class Target833
{
public Target833(string value1, string value2)
{
Value1 = value1;
Value2 = value2;
}

public string Value1 { get; }

public string Value2 { get; }
}

public sealed record Database746(
string Server = "",
string Name = "",
Expand Down
4 changes: 4 additions & 0 deletions src/Mapster.Tests/WhenUsingNonDefaultConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public void Map_To_Existing_Destination_Instance_Should_Pass()
dto.Unmapped.ShouldBe("unmapped");
}

/// <summary>
/// ignore after implement fix https://github.com/MapsterMapper/Mapster/issues/883
/// </summary>
[Ignore]
[TestMethod]
public void Map_To_Destination_Type_Without_Default_Constructor_Shoud_Throw_Exception()
{
Expand Down
9 changes: 7 additions & 2 deletions src/Mapster/Adapters/ClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ protected override Expression CreateInstantiationExpression(Expression source, E
: arg.DestinationType;
if (destType == null)
return base.CreateInstantiationExpression(source, destination, arg);
classConverter = destType.GetConstructors()

var constructors = destType.GetConstructors();
classConverter = constructors
.OrderByDescending(it => it.GetParameters().Length)
.Select(it => GetConstructorModel(it, true))
.Select(it => CreateClassConverter(source, it, arg, ctorMapping:true))
.Select(it => CreateClassConverter(source, it, arg, ctorMapping: true))
.FirstOrDefault(it => it != null);

if (classConverter == null && constructors.Length > 0)
classConverter = CreateClassConverter(source, GetConstructorModel(constructors[0], false), arg, ctorMapping: true);
}
else
{
Expand Down
Loading