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
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ INewInstanceMapping valueMapping
ctx.InstanceConstructors.TryBuildObjectFactory(collectionInfos.Source.Type, ctx.Target, out constructor);
}

if (constructor == null && !ctx.SymbolAccessor.HasAnyAccessibleConstructor(ctx.Target))
return null;

return new ForEachSetDictionaryMapping(
constructor,
collectionInfos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ INewInstanceMapping elementMapping
ctx.InstanceConstructors.TryBuildObjectFactory(collectionInfos.Source.Type, ctx.Target, out constructor);
}

if (constructor == null && !ctx.SymbolAccessor.HasAnyAccessibleConstructor(ctx.Target))
return null;

return new ForEachAddEnumerableMapping(
constructor,
collectionInfos,
Expand Down
9 changes: 7 additions & 2 deletions test/Riok.Mapperly.Tests/Mapping/DictionaryCustomTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,15 @@ public Task CustomDictionaryWithList()
}

[Fact]
public Task DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic()
public void DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic()
{
var source = TestSourceBuilder.Mapping("IDictionary<string, int>", "A", "class A : Dictionary<string, int> { private A(){} }");
return TestHelper.VerifyGenerator(source);
TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveDiagnostic(DiagnosticDescriptors.NoConstructorFound, "A has no accessible constructor with mappable arguments")
.HaveAssertedAllDiagnostics()
.HaveMapMethodBody("""throw new global::System.NotImplementedException("Mapperly: Could not generate mapping");""");
}

[Fact]
Expand Down
15 changes: 15 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ public Task MapToReadOnlyCollectionProperty()
return TestHelper.VerifyGenerator(source);
}

[Theory]
[InlineData("IEnumerable<int>", "ICollection<long>")]
[InlineData("IDictionary<int, int>", "IDictionary<int, long>")]
public void CustomCollectionInterfaceWithoutFactoryCannotBeConstructed(string sourceType, string baseInterface)
{
var source = TestSourceBuilder.Mapping(sourceType, "ITarget", $"public interface ITarget : {baseInterface} {{ }}");

TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveDiagnostic(DiagnosticDescriptors.NoConstructorFound)
.HaveMapMethodBody("""throw new global::System.NotImplementedException("Mapperly: Could not generate mapping");""")
.HaveAssertedAllDiagnostics();
}

[Fact]
public void EnumerableMappingDisabledShouldDiagnostic()
{
Expand Down

This file was deleted.

This file was deleted.

Loading