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
14 changes: 4 additions & 10 deletions src/AutoMapper.Extensions.ExpressionMapping/TypeMappingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,14 @@ public Type ReplaceType(Type sourceType)
}
else if (sourceType.IsGenericType)
{
if (TypeMappings.TryGetValue(sourceType, out Type destType))
return destType;
else
{
return sourceType.GetGenericTypeDefinition().MakeGenericType
return TypeMappings.TryGetValue(sourceType, out Type destType)
? destType
: sourceType.GetGenericTypeDefinition().MakeGenericType
(
[.. sourceType
.GetGenericArguments()
.Select(ReplaceType)]
);
}
}
else
{
Expand Down Expand Up @@ -182,11 +179,8 @@ private void FindChildPropertyTypeMaps(Type source, Type dest)
FindMaps([.. typeMap.PropertyMaps]);
void FindMaps(List<PropertyMap> maps)
{
foreach (PropertyMap pm in maps)
foreach (PropertyMap pm in maps.Where(p => p.SourceMembers.Length > 0 || p.CustomMapExpression != null))
{
if (pm.SourceMembers.Length < 1 && pm.CustomMapExpression == null)
continue;

AddChildMappings
(
source.GetFieldOrProperty(pm.DestinationMember.Name).GetMemberType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using AutoMapper.Extensions.ExpressionMapping.Structures;
using AutoMapper.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -440,10 +439,9 @@ Expression DoVisitBinary(Expression newLeft, Expression newRight, Expression con
{
if (newLeft != node.Left || newRight != node.Right || conversion != node.Conversion)
{
if (node.NodeType == ExpressionType.Coalesce && node.Conversion != null)
return Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression);
else
return Expression.MakeBinary
return node.NodeType == ExpressionType.Coalesce && node.Conversion != null
? Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression)
: Expression.MakeBinary
(
node.NodeType,
newLeft,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,21 @@ private void Should_Validate()
[Fact]
public void GrandParent_Mapping_To_Sub_Sub_Property_Condition()
{
Expression<Func<GrandParentDTO, bool>> _predicateExpression = gp => gp.Parent.Children.Any(c => c.ID2 == 3);
var expression = Mapper.Map<Expression<Func<GrandParent, bool>>>(_predicateExpression);
Expression<Func<GrandParentDTO, bool>> predicateExpression = gp => gp.Parent.Children.Any(c => c.ID2 == 3);
var expression = Mapper.Map<Expression<Func<GrandParent, bool>>>(predicateExpression);
var items = new[] {new GrandParent(){Parent = new Parent(){Children = [new Child(){ID2 = 3}], Child = new Child(){ID2 = 3}}}}.AsQueryable();
items.Where(expression).ShouldContain(items.First());
var items2 = items.UseAsDataSource(Mapper).For<GrandParentDTO>().Where(_predicateExpression);
var items2 = items.UseAsDataSource(Mapper).For<GrandParentDTO>().Where(predicateExpression);
items2.Count().ShouldBe(1);
When_Use_Outside_Class_Method_Call();
}

[Fact]
public void GrandParent_Mapping_To_Sub_Sub_Property_Condition2()
{
Expression<Func<IQueryable<GrandParentDTO>, bool>> _predicateExpression = gps => gps.Any(gp => gp.Parent.Children.Any(c => c.ID_ == 3));
Expression<Func<IQueryable<GrandParentDTO>, bool>> predicateExpression = gps => gps.Any(gp => gp.Parent.Children.Any(c => c.ID_ == 3));
Expression<Func<IQueryable<GrandParentDTO>, IQueryable<GrandParentDTO>>> _predicateExpression2 = gps => gps.Where(gp => gp.Parent.Children.Any(c => c.ID_ == 3));
var expression = Mapper.Map<Expression<Func<IQueryable<GrandParent>, bool>>>(_predicateExpression);
var expression = Mapper.Map<Expression<Func<IQueryable<GrandParent>, bool>>>(predicateExpression);
var expression1 = Mapper.Map<Expression<Func<IQueryable<GrandParent>, IQueryable<GrandParent>>>>(_predicateExpression2);
expression.ShouldNotBeNull();
expression1.ShouldNotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void Shoud_support_IEnumerable_result()
.Where(s => s.DestValue > 6);

List<Destination> list = [.. result];
Assert.NotEmpty(list);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ public void GetDeclaredProperties_ReturnsAllDeclaredProperties()

private class ClassWithStaticMembers
{
public static int StaticField = 0;
public int InstanceField = 0;
public static readonly int StaticField = 0;
public readonly int InstanceField = 0;
public static int StaticProperty { get; set; }
public int InstanceProperty { get; set; }
public static void StaticMethod() { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using AutoMapper;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
Expand Down Expand Up @@ -118,8 +116,7 @@ public void Constructor_AddsMappingsFromDelegateTypes()
typeof(Func<DestModel, bool>));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceModel)));
Assert.Equal(typeof(DestModel), manager.TypeMappings[typeof(SourceModel)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceModel), typeof(DestModel)), manager.TypeMappings);
}

#endregion
Expand All @@ -143,8 +140,7 @@ public void AddTypeMapping_SimpleTypes_AddsMapping()
manager.AddTypeMapping(typeof(SourceChild), typeof(DestChild));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceChild)));
Assert.Equal(typeof(DestChild), manager.TypeMappings[typeof(SourceChild)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceChild), typeof(DestChild)), manager.TypeMappings);
}

[Fact]
Expand Down Expand Up @@ -187,8 +183,7 @@ public void AddTypeMapping_ExpressionTypes_UnwrapsAndAddsMapping()
typeof(Expression<Func<DestChild, bool>>));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(Func<SourceChild, bool>)));
Assert.Equal(typeof(Func<DestChild, bool>), manager.TypeMappings[typeof(Func<SourceChild, bool>)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(Func<SourceChild, bool>), typeof(Func<DestChild, bool>)), manager.TypeMappings);
}

[Fact]
Expand Down Expand Up @@ -231,9 +226,8 @@ public void AddTypeMapping_ListTypes_AddsUnderlyingTypeMappings()
manager.AddTypeMapping(typeof(List<SourceItem>), typeof(List<DestItem>));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(List<SourceItem>)));
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceItem)));
Assert.Equal(typeof(DestItem), manager.TypeMappings[typeof(SourceItem)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceItem), typeof(DestItem)), manager.TypeMappings);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(List<SourceItem>), typeof(List<DestItem>)), manager.TypeMappings);
}

[Fact]
Expand All @@ -254,9 +248,8 @@ public void AddTypeMapping_ArrayTypes_AddsUnderlyingTypeMappings()
manager.AddTypeMapping(typeof(SourceItem[]), typeof(DestItem[]));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceItem[])));
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceItem)));
Assert.Equal(typeof(DestItem), manager.TypeMappings[typeof(SourceItem)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceItem), typeof(DestItem)), manager.TypeMappings);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceItem[]), typeof(DestItem[])), manager.TypeMappings);
}

#endregion
Expand Down Expand Up @@ -589,8 +582,7 @@ public void AddTypeMappingsFromDelegates_MatchingArgumentCounts_AddsMappings()
typeof(Func<DestChild, string>));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceChild)));
Assert.Equal(typeof(DestChild), manager.TypeMappings[typeof(SourceChild)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceChild), typeof(DestChild)), manager.TypeMappings);
}

[Fact]
Expand Down Expand Up @@ -633,8 +625,7 @@ public void AddTypeMappingsFromDelegates_ActionTypes_AddsMappings()
typeof(Action<DestChild>));

// Assert
Assert.True(manager.TypeMappings.ContainsKey(typeof(SourceChild)));
Assert.Equal(typeof(DestChild), manager.TypeMappings[typeof(SourceChild)]);
Assert.Contains(new KeyValuePair<Type, Type>(typeof(SourceChild), typeof(DestChild)), manager.TypeMappings);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public static implicit operator Source(int i)

public override readonly int GetHashCode()
{
return this.val.GetHashCode();
return this.val;
}

public override readonly bool Equals(object obj)
Expand Down Expand Up @@ -370,7 +370,7 @@ public static implicit operator Dest(int i)

public override readonly int GetHashCode()
{
return this.val.GetHashCode();
return this.val;
}

public override readonly bool Equals(object obj)
Expand Down Expand Up @@ -417,7 +417,7 @@ public override readonly bool Equals(object obj)
}
public override readonly int GetHashCode()
{
return Year.GetHashCode();
return Year;
}
}

Expand All @@ -444,7 +444,7 @@ public override readonly bool Equals(object obj)
}
public override readonly int GetHashCode()
{
return Year.GetHashCode();
return Year;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ public void Map_where_multiple_arguments()

//Act
Expression<Func<User, Account, object>> selectionMapped = mapper.Map<Expression<Func<User, Account, object>>>(selection);
object val = selectionMapped.Compile().Invoke(Users.ToList()[0], Users.ToList()[0].Account);
object valLocal = selectionMapped.Compile().Invoke(Users.ToList()[0], Users.ToList()[0].Account);

//Assert
Assert.False((bool)val);
Assert.False((bool)valLocal);
}

[Fact]
Expand Down Expand Up @@ -838,9 +838,9 @@ public void Can_map_expressions_with_action_independent_of_expression_param()
}

object val;
void CallSomeAction<T>(T val)
void CallSomeAction<T>(T valParam)
{
this.val = val;
this.val = valParam;
}

[Fact]
Expand Down