Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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 = true) => _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 = true) => PropertyMapActions.Add(pm => pm.ExplicitExpansion = pm.ExplicitExpansion == null ? value : pm.ExplicitExpansion);
public void Ignore() => Ignore(ignorePaths: true);
public void Ignore(bool ignorePaths)
{
Expand Down
111 changes: 111 additions & 0 deletions src/IntegrationTests/ExplicitExpansion/DisableExplicitExpansion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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 Dto
{
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, Dto>()
.ForMember(dto => dto.Code, conf => conf.ExplicitExpansion(false))
.ForAllMembers(conf => conf.ExplicitExpansion())
;});

[Fact]
public void Should_CodeBeExpanded() {
using (var ctx = new Context()) {
var dto = ProjectTo<Dto>(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.ShouldBe(_iqf.Code);
sqlSelect.SqlShouldNotSelectColumn(nameof(_iqf.Desc)); dto.Desc.ShouldBeNull();
}
}

[Fact]
public void Should_NameAndCodeBeExpanded() {
using (var ctx = new Context()) {
var dto = ProjectTo<Dto>(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));
}
}

}

public class ConstructorNoExplicitExpansion : IntegrationTest<ConstructorNoExplicitExpansion.DatabaseInitializer> {
public class Entity {
public int Id { get; set; }
public string Name { get; set; }
}
record Dto(string Name) { }
public class Context : LocalDbContext {
public DbSet<Entity> Entities { get; set; }
}
public class DatabaseInitializer : DropCreateDatabaseAlways<Context> {
protected override void Seed(Context context) {
context.Entities.Add(new() { Name = "Name" });
base.Seed(context);
}
}
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateProjection<Entity, Dto>().ForCtorParam("Name", o => o.ExplicitExpansion(false)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use mapping inheritance, the same as the test below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lbargaoanu i added a new test for constructor mapping with inheritance (sorry for the long delay i needed to change my computer)

[Fact]
public void Should_work() {
using var context = new Context();
var dto = ProjectTo<Dto>(context.Entities).Single();
dto.Name.ShouldBe("Name");
dto = ProjectTo<Dto>(context.Entities, null, d => d.Name).Single();
dto.Name.ShouldBe("Name");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
namespace AutoMapper.IntegrationTests.ExplicitExpansion;

public class ExpandCollectionsWithoutExplicit : IntegrationTest<ExpandCollectionsWithoutExplicit.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; }
}

}