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
17 changes: 17 additions & 0 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 System.Text.Json;
using static Mapster.Tests.WhenExplicitMappingRequired;
using static Mapster.Tests.WhenMappingDerived;

Expand Down Expand Up @@ -522,6 +523,22 @@ public void ClassCtorActivateDefaultValue()
});
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/911
/// </summary>
[TestMethod]
public void NotSelfCreationTypeMappingToSelfWithOutError()
{
var src = new Uri("https://www.google.com/");
var srcJ = JsonDocument.Parse("{\"key\": \"value\"}");

var result = src.Adapt<Uri>();
var resultJ = srcJ.Adapt<JsonDocument>();

result.ToString().ShouldBe("https://www.google.com/");
resultJ.RootElement.GetProperty("key").ToString().ShouldBe("value");
}

#region NowNotWorking

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/ClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Mapster.Adapters
/// </remarks>
internal class ClassAdapter : BaseClassAdapter
{
protected override int Score => -150;
protected override int Score => -200;

protected override bool CanMap(PreCompileArgument arg)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Mapster/Adapters/NotSelfCreationAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Mapster.Adapters
{
/// <summary>
/// Immitation behavior in 7.4.0 for Types that cannot be instantiated from itselves
/// Example: Uri, JsonDocument
/// </summary>
internal class NotSelfCreationAdapter : PrimitiveAdapter
{
protected override int Score => -150;

protected override bool CanMap(PreCompileArgument arg)
{
return !arg.ExplicitMapping && arg.SourceType == arg.DestinationType && arg.DestinationType.IsNotSelfCreation();
}
}
}
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/PrimitiveAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mapster.Adapters
{
internal class PrimitiveAdapter : BaseAdapter
{
protected override int Score => -200; //must do last
protected override int Score => -210; //must do last

protected override bool CanMap(PreCompileArgument arg)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Mapster/TypeAdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ private static List<TypeAdapterRule> CreateRuleTemplate()
{
return new List<TypeAdapterRule>
{
new PrimitiveAdapter().CreateRule(), //-200
new ClassAdapter().CreateRule(), //-150
new PrimitiveAdapter().CreateRule(), //-210
new ClassAdapter().CreateRule(), //-200
new NotSelfCreationAdapter().CreateRule(), //-150
new RecordTypeAdapter().CreateRule(), //-149
new ReadOnlyInterfaceAdapter().CreateRule(), // -148
new CollectionAdapter().CreateRule(), //-125
Expand Down
13 changes: 13 additions & 0 deletions src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,18 @@ public static bool IsMapsterImmutable(this Type type)
{
return type.IsMapsterPrimitive() || type.IsRecordType();
}

public static bool IsNotSelfCreation(this Type type)
{
if (type.IsMapsterPrimitive())
return false;
if(type.IsCollectionCompatible())
return false;

if (type == typeof(Type) || type.BaseType == typeof(MulticastDelegate))
return true;

return type.GetFieldsAndProperties().Any(it => (it.SetterModifier & (AccessModifier.Public | AccessModifier.NonPublic)) == 0);
}
}
}
Loading