From 85df08dc2ce47659953cd3f641479100b133d4e3 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 24 Mar 2026 10:03:04 +0800 Subject: [PATCH] Extract DefaultMaxDepth to AbpAutoMapperOptions for configurability --- .../Abp/AutoMapper/AbpAutoMapperModule.cs | 13 ++++--- .../Abp/AutoMapper/AbpAutoMapperOptions.cs | 7 ++++ .../AbpAutoMapperModule_MaxDepth_Tests.cs | 37 +++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs index fbc80d20b5..71dd37d259 100644 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs +++ b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs @@ -40,13 +40,16 @@ public class AbpAutoMapperModule : AbpModule configurator(autoMapperConfigurationContext); } - mapperConfigurationExpression.Internal().ForAllMaps((typeMap, _) => + if (options.DefaultMaxDepth.HasValue) { - if (typeMap.MaxDepth == 0) + mapperConfigurationExpression.Internal().ForAllMaps((typeMap, _) => { - typeMap.MaxDepth = 64; - } - }); + if (typeMap.MaxDepth == 0) + { + typeMap.MaxDepth = options.DefaultMaxDepth.Value; + } + }); + } var mapperConfiguration = new MapperConfiguration(mapperConfigurationExpression); diff --git a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs index e5cf1a0556..f6ab5a5408 100644 --- a/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs +++ b/framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs @@ -12,6 +12,13 @@ public class AbpAutoMapperOptions public ITypeList ValidatingProfiles { get; set; } + /// + /// Default MaxDepth applied to all maps that don't have an explicit MaxDepth configured. + /// Set to null to disable the default MaxDepth behavior. + /// Default: 64. + /// + public int? DefaultMaxDepth { get; set; } = 64; + public AbpAutoMapperOptions() { Configurators = new List>(); diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_MaxDepth_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_MaxDepth_Tests.cs index ad24430cbd..d7cf32913b 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_MaxDepth_Tests.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_MaxDepth_Tests.cs @@ -63,3 +63,40 @@ public class AbpAutoMapperModule_CustomMaxDepth_Tests : AbpIntegratedTest +{ + private readonly IConfigurationProvider _configurationProvider; + + public AbpAutoMapperModule_DisabledMaxDepth_Tests() + { + _configurationProvider = ServiceProvider.GetRequiredService(); + } + + [Fact] + public void Should_Not_Set_MaxDepth_When_Disabled() + { + var typeMap = _configurationProvider.Internal().FindTypeMapFor(); + typeMap.ShouldNotBeNull(); + typeMap.MaxDepth.ShouldBe(0); + } + + [DependsOn( + typeof(AbpAutoMapperModule), + typeof(AbpObjectExtendingTestModule) + )] + public class TestModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + Configure(options => + { + options.DefaultMaxDepth = null; + options.Configurators.Add(ctx => + { + ctx.MapperConfiguration.CreateMap(); + }); + }); + } + } +}