-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent crash for delegate fields when deep mapping is enabled (#…
- Loading branch information
Showing
8 changed files
with
111 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
src/Riok.Mapperly/Descriptors/MappingBuilders/SpecialTypeMappingBuilder.cs
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/Riok.Mapperly/Descriptors/MappingBuilders/ToObjectMappingBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
using Riok.Mapperly.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilders; | ||
|
||
public static class ToObjectMappingBuilder | ||
{ | ||
public static NewInstanceMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (!ctx.IsConversionEnabled(MappingConversionType.ExplicitCast)) | ||
return null; | ||
|
||
if (ctx.Target.SpecialType != SpecialType.System_Object) | ||
return null; | ||
|
||
if (!ctx.MapperConfiguration.UseDeepCloning) | ||
return new CastMapping(ctx.Source, ctx.Target); | ||
|
||
if (ctx.Source.SpecialType == SpecialType.System_Object) | ||
{ | ||
ctx.ReportDiagnostic(DiagnosticDescriptors.MappedObjectToObjectWithoutDeepClone, ctx.Source.Name, ctx.Target.Name); | ||
return new DirectAssignmentMapping(ctx.Source); | ||
} | ||
|
||
return new CastMapping(ctx.Source, ctx.Target, ctx.FindOrBuildMapping(ctx.Source, ctx.Source)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Riok.Mapperly.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Tests.Mapping; | ||
|
||
public class DelegateTest | ||
{ | ||
[Fact] | ||
public void ActionToAction() | ||
{ | ||
var source = TestSourceBuilder.Mapping("Action<string>", "Action<string>"); | ||
TestHelper.GenerateMapper(source).Should().HaveMapMethodBody("return source;"); | ||
} | ||
|
||
[Fact] | ||
public void ActionToActionWithDeepCloning() | ||
{ | ||
var source = TestSourceBuilder.Mapping("Action<string>", "Action<string>", TestSourceBuilderOptions.WithDeepCloning); | ||
TestHelper.GenerateMapper(source).Should().HaveMapMethodBody("return source;"); | ||
} | ||
|
||
[Fact] | ||
public void FuncToFunc() | ||
{ | ||
var source = TestSourceBuilder.Mapping("Func<string, string>", "Func<string, string>"); | ||
TestHelper.GenerateMapper(source).Should().HaveMapMethodBody("return source;"); | ||
} | ||
|
||
[Fact] | ||
public void FuncToFuncWithDeepCloning() | ||
{ | ||
var source = TestSourceBuilder.Mapping("Func<string>", "Func<string>", TestSourceBuilderOptions.WithDeepCloning); | ||
TestHelper.GenerateMapper(source).Should().HaveMapMethodBody("return source;"); | ||
} | ||
|
||
[Fact] | ||
public void CustomDelegateToCustomDelegate() | ||
{ | ||
var source = TestSourceBuilder.Mapping("X", "X", "delegate string X(string value);"); | ||
TestHelper.GenerateMapper(source).Should().HaveMapMethodBody("return source;"); | ||
} | ||
|
||
[Fact] | ||
public void CustomDelegateToCustomDelegateWithDeepCloning() | ||
{ | ||
var source = TestSourceBuilder.Mapping("X", "X", TestSourceBuilderOptions.WithDeepCloning, "delegate string X(string value);"); | ||
TestHelper.GenerateMapper(source).Should().HaveMapMethodBody("return source;"); | ||
} | ||
|
||
[Fact] | ||
public void FuncToCustomDelegateShouldDiagnostic() | ||
{ | ||
var source = TestSourceBuilder.Mapping("Func<string, string>", "X", "delegate string X(string value);"); | ||
TestHelper | ||
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics) | ||
.Should() | ||
.HaveDiagnostic(DiagnosticDescriptors.CouldNotCreateMapping) | ||
.HaveAssertedAllDiagnostics(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...Mapperly.Tests/Mapping/SpecialTypeTest.cs → ...apperly.Tests/Mapping/ToObjectTypeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters