Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude explicit casts when source type is object #400

Merged
merged 1 commit into from
May 2, 2023
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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));
}
}