Skip to content

Commit

Permalink
fix: exclude explicit casts when source type is object (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed May 2, 2023
1 parent b13d364 commit 3af9cc9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
@@ -1,3 +1,4 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Descriptors.Mappings;
Expand All @@ -15,6 +16,9 @@ public static class ExplicitCastMappingBuilder
if (ctx.MapperConfiguration.UseDeepCloning && !ctx.Source.IsImmutable() && !ctx.Target.IsImmutable())
return null;

if (SymbolEqualityComparer.Default.Equals(ctx.Source, ctx.Compilation.ObjectType))
return null;

var conversion = ctx.Compilation.ClassifyConversion(ctx.Source, ctx.Target);

// only allow user defined explicit reference conversions
Expand Down
10 changes: 10 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/CastTest.cs
Expand Up @@ -363,4 +363,14 @@ public void ExplicitCastMappingDisabledShouldDiagnostic()
.Should()
.HaveDiagnostic(new(DiagnosticDescriptors.CouldNotCreateMapping));
}

[Fact]
public void FromObjectExplicitCastShouldBeIgnoredAndDiagnostic()
{
var source = TestSourceBuilder.Mapping("object", "byte");
TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveDiagnostic(new(DiagnosticDescriptors.CouldNotCreateMapping));
}
}

0 comments on commit 3af9cc9

Please sign in to comment.