diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/DictionaryMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/DictionaryMappingBuilder.cs index ece07b76f0..615231c12c 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilders/DictionaryMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/DictionaryMappingBuilder.cs @@ -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, diff --git a/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs b/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs index 627f74ea72..d8c67e2d6a 100644 --- a/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs +++ b/src/Riok.Mapperly/Descriptors/MappingBuilders/EnumerableMappingBuilder.cs @@ -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, diff --git a/test/Riok.Mapperly.Tests/Mapping/DictionaryCustomTest.cs b/test/Riok.Mapperly.Tests/Mapping/DictionaryCustomTest.cs index 71c9e6d23d..ea6a698b3d 100644 --- a/test/Riok.Mapperly.Tests/Mapping/DictionaryCustomTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/DictionaryCustomTest.cs @@ -185,10 +185,15 @@ public Task CustomDictionaryWithList() } [Fact] - public Task DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic() + public void DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic() { var source = TestSourceBuilder.Mapping("IDictionary", "A", "class A : Dictionary { 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] diff --git a/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs b/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs index 1230bb0c31..05e0a144d6 100644 --- a/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/EnumerableTest.cs @@ -658,6 +658,21 @@ public Task MapToReadOnlyCollectionProperty() return TestHelper.VerifyGenerator(source); } + [Theory] + [InlineData("IEnumerable", "ICollection")] + [InlineData("IDictionary", "IDictionary")] + 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() { diff --git a/test/Riok.Mapperly.Tests/_snapshots/DictionaryCustomTest.DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic#Mapper.g.verified.cs b/test/Riok.Mapperly.Tests/_snapshots/DictionaryCustomTest.DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic#Mapper.g.verified.cs deleted file mode 100644 index b2eeb67988..0000000000 --- a/test/Riok.Mapperly.Tests/_snapshots/DictionaryCustomTest.DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic#Mapper.g.verified.cs +++ /dev/null @@ -1,17 +0,0 @@ -//HintName: Mapper.g.cs -// -#nullable enable -public partial class Mapper -{ - [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")] - private partial global::A Map(global::System.Collections.Generic.IDictionary source) - { - var target = new global::A(); - target.EnsureCapacity(source.Count); - foreach (var item in source) - { - target[item.Key] = item.Value; - } - return target; - } -} \ No newline at end of file diff --git a/test/Riok.Mapperly.Tests/_snapshots/DictionaryCustomTest.DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic.verified.txt b/test/Riok.Mapperly.Tests/_snapshots/DictionaryCustomTest.DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic.verified.txt deleted file mode 100644 index 9b59af6f5e..0000000000 --- a/test/Riok.Mapperly.Tests/_snapshots/DictionaryCustomTest.DictionaryToCustomDictionaryWithPrivateCtorShouldDiagnostic.verified.txt +++ /dev/null @@ -1,23 +0,0 @@ -{ - Diagnostics: [ - { - Location: /* -{ - private partial A Map(IDictionary source); - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -} -*/ - : (11,4)-(11,59), - Message: A has no accessible constructor with mappable arguments, - Severity: Error, - Descriptor: { - Id: RMG013, - Title: No accessible constructor with mappable arguments found, - MessageFormat: {0} has no accessible constructor with mappable arguments, - Category: Mapper, - DefaultSeverity: Error, - IsEnabledByDefault: true - } - } - ] -} \ No newline at end of file