Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)' 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="value">Is explicitExpansion active</param>
void ExplicitExpansion(bool value = 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(bool value) => _ctorParamActions.Add(cpm => cpm.ExplicitExpansion = value);

public void Configure(TypeMap typeMap)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ public interface IProjectionMemberConfiguration<TSource, TDestination, TMember>
/// <summary>
/// Ignore this member for LINQ projections unless explicitly expanded during projection
/// </summary>
void ExplicitExpansion();
/// <param name="value">Is explicitExpansion active</param>
void ExplicitExpansion(bool value = 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(bool value) => PropertyMapActions.Add(pm => pm.ExplicitExpansion = value);
public void Ignore() => Ignore(ignorePaths: true);
public void Ignore(bool ignorePaths)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace AutoMapper.IntegrationTests.ExplicitExpansion;

public class ConstructorExplicitExpansionOverride : IntegrationTest<ConstructorExplicitExpansionOverride.DatabaseInitializer> {
public class Entity {
public int Id { get; set; }
public string Name { get; set; }
}

public class SubEntity : Entity {
}

record Dto(string Name);
record SubDto(string Name) : Dto(Name) { }

public class Context : LocalDbContext {
public DbSet<Entity> Entities { get; set; }
public DbSet<SubEntity> SubEntities { get; set; }
}
public class DatabaseInitializer : DropCreateDatabaseAlways<Context> {
protected override void Seed(Context context) {
context.Entities.Add(new() { Name = "base" });
context.SubEntities.Add(new() { Name = "derived" });
base.Seed(context);
}
}
protected override MapperConfiguration CreateConfiguration() => new(c => {
c.CreateMap<Entity, Dto>().ForCtorParam("Name", o => o.ExplicitExpansion());
c.CreateMap<SubEntity, SubDto>().IncludeBase<Entity, Dto>().ForCtorParam("Name", o => o.ExplicitExpansion(false));
});
[Fact]
public void Should_work() {
using var context = new Context();
var dtos = ProjectTo<Dto>(context.Entities).ToList();
dtos.Count.ShouldBe(2);
dtos[0].ShouldBeOfType<Dto>().Name.ShouldBeNull();
dtos[1].ShouldBeOfType<SubDto>().Name.ShouldBe("derived");
}
}
110 changes: 110 additions & 0 deletions src/IntegrationTests/ExplicitExpansion/ExpandCollectionsOverride.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
namespace AutoMapper.IntegrationTests.ExplicitExpansion;

public class ExpandCollectionsOverride : IntegrationTest<ExpandCollectionsOverride.DatabaseInitializer>
{
TrainingCourseDto _course;

protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Category, CategoryDto>();
cfg.CreateMap<TrainingCourse, TrainingCourseDto>()
.ForMember(p => p.CourseName, opt => opt.ExplicitExpansion());
cfg.CreateMap<TrainingContent, TrainingContentDto>().ForMember(c => c.Category, o => o.ExplicitExpansion());
cfg.CreateMap<TrainingCourse, TrainingCourseDetailDto>()
.IncludeBase<TrainingCourse, TrainingCourseDto>()
.ForMember(c => c.CourseName, opt => opt.ExplicitExpansion(false));
});

[Fact]
public void Should_notexpand_courseName() {
using (var context = new ClientContext()) {
_course = ProjectTo<TrainingCourseDto>(context.TrainingCourses).FirstOrDefault();
}
_course.CourseName.ShouldBeNull();
}

[Fact]
public void Should_expand_courseName() {
using (var context = new ClientContext()) {
_course = ProjectTo<TrainingCourseDetailDto>(context.TrainingCourses).FirstOrDefault();
}
_course.CourseName.ShouldBe("Course 1");
}

public class DatabaseInitializer : DropCreateDatabaseAlways<ClientContext>
{
protected override void Seed(ClientContext context)
{
var category = new Category { CategoryName = "Category 1" };
var course = new TrainingCourse { CourseName = "Course 1" };
context.TrainingCourses.Add(course);
var content = new TrainingContent { ContentName = "Content 1", Category = category };
context.TrainingContents.Add(content);
course.Content.Add(content);
}
}

public class ClientContext : LocalDbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<TrainingCourse> TrainingCourses { get; set; }
public DbSet<TrainingContent> TrainingContents { get; set; }
}

public class TrainingCourse
{
[Key]
public int CourseId { get; set; }

public string CourseName { get; set; }

public virtual IList<TrainingContent> Content { get; set; } = new List<TrainingContent>();
}

public class TrainingContent
{
[Key]
public int ContentId { get; set; }

public string ContentName { get; set; }
public string CaptionName { get; set; }

public Category Category { get; set; }
}

public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}


public class TrainingCourseDto
{
public int CourseId { get; set; }

public string CourseName { get; set; }

public virtual IList<TrainingContentDto> Content { get; set; }
}

public class TrainingCourseDetailDto : TrainingCourseDto
{
}

public class CategoryDto
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}

public class TrainingContentDto
{
public int ContentId { get; set; }

public string ContentName { get; set; }

public CategoryDto Category { get; set; }
}

}