Skip to content

Fix exceptions with generic source types #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 15, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private Expression VisitAllParametersExpression<T>(Expression<T> expression)
from t in expression.Parameters
let sourceParamType = t.Type
from destParamType in _destSubTypes.Where(dt => dt != sourceParamType)
let a = destParamType.IsGenericType() ? destParamType.GetTypeInfo().GenericTypeArguments[0] : destParamType
let a = destParamType.IsEnumerableType() ? destParamType.GetGenericElementType() : destParamType
let typeMap = _configurationProvider.ResolveTypeMap(a, sourceParamType)
where typeMap != null
let oldParam = t
Expand Down
3 changes: 3 additions & 0 deletions src/AutoMapper.Extensions.ExpressionMapping/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,8 @@ public static bool IsQueryableType(this Type type)

public static Type GetGenericElementType(this Type type)
=> type.HasElementType ? type.GetElementType() : type.GetTypeInfo().GenericTypeArguments[0];

public static bool IsEnumerableType(this Type type) =>
type.IsGenericType && typeof(System.Collections.IEnumerable).IsAssignableFrom(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ public void When_Apply_Where_Clause_Over_Queryable_As_Data_Source()
result.ShouldAllBe(expOverDTO);
}

[Fact]
public void Should_Map_From_Generic_Type()
{
// Arrange
var mapper = CreateMapper();

var models = new List<GenericModel<bool>>()
{
new GenericModel<bool> {ABoolean = true},
new GenericModel<bool> {ABoolean = false},
new GenericModel<bool> {ABoolean = true},
new GenericModel<bool> {ABoolean = false}
};

var queryable = models.AsQueryable();

Expression<Func<DTO, bool>> expOverDTO = (dto) => dto.Nested.AnotherBoolean;

// Act
var q = queryable.UseAsDataSource(mapper).For<DTO>().Where(expOverDTO);

var result = q.ToList();

// Assert
result.ShouldNotBeNull();
result.Count.ShouldBe(2);
result.ShouldAllBe(expOverDTO);
}

private static IMapper CreateMapper()
{
var mapperConfig = new MapperConfiguration(cfg =>
Expand All @@ -52,7 +81,10 @@ private static IMapper CreateMapper()
.ForMember(d => d.AnotherBoolean, opt => opt.MapFrom(s => s.ABoolean));
cfg.CreateMap<DTO, Model>()
.ForMember(d => d.ABoolean, opt => opt.MapFrom(s => s.Nested.AnotherBoolean));

cfg.CreateMap<GenericModel<bool>, DTO>()
.ForMember(d => d.Nested, opt => opt.MapFrom(s => s));
cfg.CreateMap<GenericModel<bool>, DTO.DTONested>()
.ForMember(d => d.AnotherBoolean, opt => opt.MapFrom(s => s.ABoolean));
});

var mapper = mapperConfig.CreateMapper();
Expand All @@ -73,5 +105,11 @@ private class Model
{
public bool ABoolean { get; set; }
}


private class GenericModel<T>
{
public T ABoolean { get; set; }
}
}
}