diff --git a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs index bbc270317a..844a679e3e 100644 --- a/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs @@ -18,6 +18,101 @@ public Task ReferenceLoopInitProperty() return TestHelper.VerifyGenerator(source); } + [Fact] + public Task SetRecursiveDepthForLoop() + { + var source = TestSourceBuilder.Mapping( + "System.Linq.IQueryable", + "System.Linq.IQueryable", + TestSourceBuilderOptions.Default with + { + MaxRecursiveDepth = 2 + }, + "class A { public A? Parent { get; set; } }", + "class B { public B? Parent { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + + [Fact] + public Task MethodAttributeSetRecursiveDepthForLoop() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "[MapperMaxRecursiveDepth(2)] " + + "partial System.Linq.IQueryable Map(System.Linq.IQueryable src);" + + "[MapperMaxRecursiveDepth(2)] partial B Map(A src);", + "class A { public A? Parent { get; set; } }", + "class B { public B? Parent { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + + [Fact] + public Task MethodAttributeOverridesClassSetRecursiveDepthForLoop() + { + var source = TestSourceBuilder.MapperWithBodyAndTypes( + "[MapperMaxRecursiveDepth(2)] " + + "partial System.Linq.IQueryable Map(System.Linq.IQueryable src);" + + "[MapperMaxRecursiveDepth(2)] partial B Map(A src);", + TestSourceBuilderOptions.Default with + { + MaxRecursiveDepth = 4 + }, + "class A { public A? Parent { get; set; } }", + "class B { public B? Parent { get; set; } }" + ); + + return TestHelper.VerifyGenerator(source); + } + + [Fact] + public Task AssemblyDefaultShouldWork() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + + [assembly: MapperDefaultsAttribute(MaxRecursiveDepth = 2)] + [Mapper()] + public partial class MyMapper + { + private partial B Map(A source); + } + + class A { public A? Parent { get; set; } } + + class B { public B? Parent { get; set; } } + """ + ); + + return TestHelper.VerifyGenerator(source); + } + + [Fact] + public Task AttributeShouldOverrideAssemblyDefault() + { + var source = TestSourceBuilder.CSharp( + """ + using Riok.Mapperly.Abstractions; + + [assembly: MapperDefaultsAttribute(MaxRecursiveDepth = 2)] + [Mapper(MaxRecursiveDepth = 4)] + public partial class MyMapper + { + private partial B Map(A source); + } + + class A { public A? Parent { get; set; } } + + class B { public B? Parent { get; set; } } + """ + ); + + return TestHelper.VerifyGenerator(source); + } + [Fact] public Task ReferenceLoopCtor() { diff --git a/test/Riok.Mapperly.Tests/TestSourceBuilder.cs b/test/Riok.Mapperly.Tests/TestSourceBuilder.cs index 62579524b3..7ef210a5d9 100644 --- a/test/Riok.Mapperly.Tests/TestSourceBuilder.cs +++ b/test/Riok.Mapperly.Tests/TestSourceBuilder.cs @@ -92,6 +92,7 @@ private static string BuildAttribute(TestSourceBuilderOptions options) Attribute(options.IgnoreObsoleteMembersStrategy), Attribute(options.RequiredMappingStrategy), Attribute(options.IncludedMembers), + Attribute(options.MaxRecursiveDepth), }.WhereNotNull(); return $"[Mapper({string.Join(", ", attrs)})]"; @@ -109,6 +110,9 @@ private static string BuildAttribute(TestSourceBuilderOptions options) private static string? Attribute(bool? value, [CallerArgumentExpression("value")] string? expression = null) => value.HasValue ? Attribute(value.Value ? "true" : "false", expression) : null; + private static string? Attribute(int? value, [CallerArgumentExpression("value")] string? expression = null) => + value.HasValue ? Attribute(value.Value.ToString(), expression) : null; + private static string? Attribute(string? value, [CallerArgumentExpression("value")] string? expression = null) { if (value == null) diff --git a/test/Riok.Mapperly.Tests/TestSourceBuilderOptions.cs b/test/Riok.Mapperly.Tests/TestSourceBuilderOptions.cs index 0060d5008d..c055ef1344 100644 --- a/test/Riok.Mapperly.Tests/TestSourceBuilderOptions.cs +++ b/test/Riok.Mapperly.Tests/TestSourceBuilderOptions.cs @@ -16,7 +16,8 @@ public record TestSourceBuilderOptions( bool? EnumMappingIgnoreCase = null, IgnoreObsoleteMembersStrategy? IgnoreObsoleteMembersStrategy = null, RequiredMappingStrategy? RequiredMappingStrategy = null, - MemberVisibility? IncludedMembers = null + MemberVisibility? IncludedMembers = null, + int? MaxRecursiveDepth = null ) { public const string DefaultMapperClassName = "Mapper";