Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add options to disable and override ExplicitExpansion
  • Loading branch information
Angelinsky7 committed Aug 3, 2023
commit dd6b4ce8104975f5383a3c83271da5c67d4758ea
8 changes: 6 additions & 2 deletions src/AutoMapper/ApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMappe
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.PropertyMapAction> AutoMapper.IProfileConfiguration.AllPropertyMapActions.get()' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<System.Action<AutoMapper.PropertyMap, AutoMapper.IMemberConfigurationExpression>> AutoMapper.IProfileConfiguration.AllPropertyMapActions.get()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyCollection<System.Action<AutoMapper.PropertyMap, AutoMapper.IMemberConfigurationExpression>> AutoMapper.IProfileConfiguration.AllPropertyMapActions.get()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public void AutoMapper.Configuration.ICtorParamConfigurationExpression.ExplicitExpansion()' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public void AutoMapper.IProjectionMemberConfiguration<TSource, TDestination, TMember>.ExplicitExpansion()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public void AutoMapper.IProjectionMemberConfiguration<TSource, TDestination, TMember>.ExplicitExpansion()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public void AutoMapper.IProjectionMemberConfiguration<TSource, TDestination, TMember>.ExplicitExpansion(System.Boolean, System.Boolean)' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public void AutoMapper.Configuration.ICtorParamConfigurationExpression.ExplicitExpansion(System.Boolean)' is present in the implementation but not in the contract.
MembersMustExist : Member 'public void AutoMapper.Configuration.MemberConfigurationExpression<TSource, TDestination, TMember>.ExplicitExpansion()' does not exist in the implementation but it does exist in the contract.
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.IgnoreAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.MapAtRuntimeAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.MappingOrderAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
Expand All @@ -13,4 +17,4 @@ CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMappe
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.ValueConverterAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
CannotChangeAttribute : Attribute 'System.AttributeUsageAttribute' on 'AutoMapper.Configuration.Annotations.ValueResolverAttribute' changed from '[AttributeUsageAttribute(384)]' in the contract to '[AttributeUsageAttribute(AttributeTargets.Field | AttributeTargets.Property)]' in the implementation.
TypeCannotChangeClassification : Type 'AutoMapper.Execution.TypeMapPlanBuilder' is a 'ref struct' in the implementation but is a 'struct' in the contract.
Total Issues: 14
Total Issues: 18
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public interface ICtorParamConfigurationExpression
/// <summary>
/// Ignore this member for LINQ projections unless explicitly expanded during projection
/// </summary>
void ExplicitExpansion();
/// <param name="explicitExpansion">Is explicitExpansion active</param>
void ExplicitExpansion(Boolean explicitExpansion = true);
}
public interface ICtorParamConfigurationExpression<TSource> : ICtorParamConfigurationExpression
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public void MapFrom(string sourceMembersPath)
_ctorParamActions.Add(cpm => cpm.MapFrom(sourceMembersPath, sourceMembers));
}

public void ExplicitExpansion() => _ctorParamActions.Add(cpm => cpm.ExplicitExpansion = true);
public void ExplicitExpansion(Boolean explicitExpansion = true) => _ctorParamActions.Add(cpm => cpm.ExplicitExpansion = explicitExpansion);

public void Configure(TypeMap typeMap)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ public interface IProjectionMemberConfiguration<TSource, TDestination, TMember>
/// <summary>
/// Ignore this member for LINQ projections unless explicitly expanded during projection
/// </summary>
void ExplicitExpansion();
/// <param name="explicitExpansion">Is explicitExpansion active</param>
/// <param name="overrideExpansion">Does explicitExpansion can override settings already set by another ExplicitExpansion call</param>
void ExplicitExpansion(Boolean explicitExpansion = true, Boolean overrideExpansion = true);
/// <summary>
/// Apply a transformation function after any resolved destination member value with the given type
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void PreConditionCore(Expression<Func<TSource, TDestination, ResolutionC
PropertyMapActions.Add(pm => pm.PreCondition = expr);
public void AddTransform(Expression<Func<TMember, TMember>> transformer) =>
PropertyMapActions.Add(pm => pm.AddValueTransformation(new ValueTransformerConfiguration(pm.DestinationType, transformer)));
public void ExplicitExpansion() => PropertyMapActions.Add(pm => pm.ExplicitExpansion = true);
public void ExplicitExpansion(Boolean explicitExpansion = true, Boolean overrideExpansion = true) => PropertyMapActions.Add(pm => pm.ExplicitExpansion = overrideExpansion || pm.ExplicitExpansion == null ? explicitExpansion : pm.ExplicitExpansion);
public void Ignore() => Ignore(ignorePaths: true);
public void Ignore(bool ignorePaths)
{
Expand Down
130 changes: 130 additions & 0 deletions src/IntegrationTests/ExplicitExpansion/DisableExplicitExpansion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System.Text.RegularExpressions;

namespace AutoMapper.IntegrationTests.ExplicitExpansion;

public class DisableExplicitExpansion : IntegrationTest<DisableExplicitExpansion.DatabaseInitializer>
{
public class Source
{ [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public Int32 Desc { get; set; }
public String Name { get; set; }
public String Code { get; set; }
}
public class DtoWithoutOverride
{
public String Name { get; set; }
public String Code { get; set; }
public Nullable<int> Desc { get; set; }
}

public class DtoWithOverride {
public String Name { get; set; }
public String Code { get; set; }
public Nullable<int> Desc { get; set; }
}

public class Context : LocalDbContext
{
public List<string> Log = new List<string>();

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.LogTo(s => Log.Add(s));
base.OnConfiguring(optionsBuilder);
}

public DbSet<Source> Sources { get; set; }

public string GetLastSelectSqlLogEntry() => Log.LastOrDefault(_ => _.Contains("SELECT"));
}

private static readonly IQueryable<Source> _iq = new List<Source> {
new Source() { Name = "Name1", Code = "Code1", Desc = -12 },
} .AsQueryable();

private static readonly Source _iqf = _iq.First();

public class DatabaseInitializer : DropCreateDatabaseAlways<Context>
{
protected override void Seed(Context context)
{
context.Sources.Add(_iqf);
base.Seed(context);
}
}

protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, DtoWithOverride>()
.ForMember(dto => dto.Desc, conf => conf.ExplicitExpansion())
.ForMember(dto => dto.Name, conf => conf.MapFrom(src => src.Name))
.ForMember(dto => dto.Code, conf => conf.ExplicitExpansion(false))
.ForAllMembers(conf => conf.ExplicitExpansion())
;

cfg.CreateMap<Source, DtoWithoutOverride>()
.ForMember(dto => dto.Desc, conf => conf.ExplicitExpansion())
.ForMember(dto => dto.Name, conf => conf.MapFrom(src => src.Name))
.ForMember(dto => dto.Code, conf => conf.ExplicitExpansion(false))
.ForAllMembers(conf => conf.ExplicitExpansion(overrideExpansion: false))
;});

[Fact]
public void NoExplicitExpansion() {
using (var ctx = new Context()) {
var dto = ProjectTo<DtoWithOverride>(ctx.Sources).ToList().First();
var sqlSelect = ctx.GetLastSelectSqlLogEntry();
sqlSelect.SqlFromShouldStartWith(nameof(ctx.Sources));
sqlSelect.ShouldNotContain("JOIN");

sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Name)); dto.Name.ShouldBeNull();
sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Code)); dto.Code.ShouldBeNull();
sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc)); dto.Desc.ShouldBeNull();
}
}

[Fact]
public void OnlyExplicitExpansionForCode()
{
using (var ctx = new Context())
{
var dto = ProjectTo<DtoWithoutOverride>(ctx.Sources).ToList().First();
var sqlSelect = ctx.GetLastSelectSqlLogEntry();
sqlSelect.SqlFromShouldStartWith(nameof(ctx.Sources));
sqlSelect.ShouldNotContain("JOIN");

sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Name)); dto.Name.ShouldBeNull();
sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Code)); dto.Code.ShouldNotBeNull();
sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc)); dto.Desc.ShouldBeNull();
}
}

[Fact]
public void ProjectNoExplicit() {
using (var ctx = new Context()) {
var dto = ProjectTo<DtoWithOverride>(ctx.Sources, null, _ => _.Name).First();
var sqlSelect = ctx.GetLastSelectSqlLogEntry();
sqlSelect.SqlFromShouldStartWith(nameof(ctx.Sources));
sqlSelect.ShouldNotContain("JOIN");

dto.Name.ShouldBe(_iqf.Name); sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Name));
dto.Code.ShouldBeNull(_iqf.Code); sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Code));
dto.Desc.ShouldBeNull(); sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc));
}
}

[Fact]
public void ProjectWithCodeImplicit() {
using (var ctx = new Context()) {
var dto = ProjectTo<DtoWithoutOverride>(ctx.Sources, null, _ => _.Name).First();
var sqlSelect = ctx.GetLastSelectSqlLogEntry();
sqlSelect.SqlFromShouldStartWith(nameof(ctx.Sources));
sqlSelect.ShouldNotContain("JOIN");

dto.Name.ShouldBe(_iqf.Name); sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Name));
dto.Code.ShouldBe(_iqf.Code); sqlSelect.SqlShouldSelectColumn(nameof(_iqf.Code));
dto.Desc.ShouldBeNull(); sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc));
}
}

}