Browse Source

Extract DefaultMaxDepth to AbpAutoMapperOptions for configurability

pull/25138/head
maliming 7 days ago
parent
commit
85df08dc2c
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 13
      framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperModule.cs
  2. 7
      framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs
  3. 37
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_MaxDepth_Tests.cs

13
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);

7
framework/src/Volo.Abp.AutoMapper/Volo/Abp/AutoMapper/AbpAutoMapperOptions.cs

@ -12,6 +12,13 @@ public class AbpAutoMapperOptions
public ITypeList<Profile> ValidatingProfiles { get; set; }
/// <summary>
/// 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.
/// </summary>
public int? DefaultMaxDepth { get; set; } = 64;
public AbpAutoMapperOptions()
{
Configurators = new List<Action<IAbpAutoMapperConfigurationContext>>();

37
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_MaxDepth_Tests.cs

@ -63,3 +63,40 @@ public class AbpAutoMapperModule_CustomMaxDepth_Tests : AbpIntegratedTest<AbpAut
}
}
}
public class AbpAutoMapperModule_DisabledMaxDepth_Tests : AbpIntegratedTest<AbpAutoMapperModule_DisabledMaxDepth_Tests.TestModule>
{
private readonly IConfigurationProvider _configurationProvider;
public AbpAutoMapperModule_DisabledMaxDepth_Tests()
{
_configurationProvider = ServiceProvider.GetRequiredService<IConfigurationProvider>();
}
[Fact]
public void Should_Not_Set_MaxDepth_When_Disabled()
{
var typeMap = _configurationProvider.Internal().FindTypeMapFor<MyEntity, MyEntityDto>();
typeMap.ShouldNotBeNull();
typeMap.MaxDepth.ShouldBe(0);
}
[DependsOn(
typeof(AbpAutoMapperModule),
typeof(AbpObjectExtendingTestModule)
)]
public class TestModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpAutoMapperOptions>(options =>
{
options.DefaultMaxDepth = null;
options.Configurators.Add(ctx =>
{
ctx.MapperConfiguration.CreateMap<MyEntity, MyEntityDto>();
});
});
}
}
}

Loading…
Cancel
Save