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
Added enum to int conversion for unmapped side of binary expressions
  • Loading branch information
ErikGjers committed Feb 11, 2023
commit 14b36267391e3ac60fd68463d39ac9114f847255
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,17 @@ Expression DoVisitBinary(Expression newLeft, Expression newRight, Expression con
{
if (newLeft != node.Left || newRight != node.Right || conversion != node.Conversion)
{
if(node.Left.Type.IsEnum && node.Right.Type.IsEnum)
{
if (newLeft.Type.IsEnum && !newRight.Type.IsEnum)
{
newLeft = Expression.Convert(newLeft, newRight.Type);
}
else if (!newLeft.Type.IsEnum && newRight.Type.IsEnum)
{
newRight = Expression.Convert(newRight, newLeft.Type);
}
}
if (node.NodeType == ExpressionType.Coalesce && node.Conversion != null)
return Expression.Coalesce(newLeft, newRight, conversion as LambdaExpression);
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using Newtonsoft.Json.Linq;
using System;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class ExpressionMappingEnumToIntOrString : AutoMapperSpecBase
{
public enum SimpleEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
private class EntityDto
{
internal SimpleEnum Value { get; set; }
}
private class EntityAsString
{
internal string Value { get; set; }
}
private class EntityAsInt
{
internal int Value { get; set; }
}
public class SimpleEnumToStringTypeResolver : ITypeConverter<SimpleEnum, string>
{
public string Convert(SimpleEnum source, string destination, ResolutionContext context)
{
return source.ToString();
}
}
protected override MapperConfiguration Configuration
{
get
{
return new MapperConfiguration(config =>
{
config.AddExpressionMapping();
config.CreateMap<EntityAsString, EntityDto>()
.ForMember(dest => dest.Value, config => config.MapFrom(src => src.Value));
config.CreateMap<EntityAsInt, EntityDto>()
.ForMember(dest => dest.Value, config => config.MapFrom(src => src.Value));
config.CreateMap<SimpleEnum, string>().ConvertUsing<SimpleEnumToStringTypeResolver>();
});
}
}

[Fact]
public void BinaryExpressionOfEnumsToInt()
{
Expression<Func<EntityAsInt, bool>> mappedExpression;
{
var param = Expression.Parameter(typeof(EntityDto), "x");
var property = Expression.Property(param, nameof(EntityDto.Value));
var constant = Expression.Constant(SimpleEnum.Value2, typeof(SimpleEnum));
var binaryExpression = Expression.Equal(property, constant);
var lambdaExpression = Expression.Lambda(binaryExpression, param);
mappedExpression = Mapper.Map<Expression<Func<EntityAsInt, bool>>>(lambdaExpression);
}

Expression<Func<EntityAsInt, bool>> translatedExpression= translatedExpression = x => x.Value == 2;

var mappedExpressionDelegate = mappedExpression.Compile();
var translatedExpressionDelegate = translatedExpression.Compile();

var entity = new EntityAsInt { Value = (int)SimpleEnum.Value2 };
var mappedResult = mappedExpressionDelegate(entity);
var translatedResult = translatedExpressionDelegate(entity);

Assert.True(translatedResult);
Assert.Equal(mappedResult, translatedResult);
}

[Fact]
public void BinaryExpressionOfEnumsToString()
{
Expression<Func<EntityAsString, bool>> mappedExpression;
{
var param = Expression.Parameter(typeof(EntityDto), "x");
var property = Expression.Property(param, nameof(EntityDto.Value));
var constant = Expression.Constant(SimpleEnum.Value2, typeof(SimpleEnum));
var binaryExpression = Expression.Equal(property, constant);
var lambdaExpression = Expression.Lambda(binaryExpression, param);
mappedExpression = Mapper.Map<Expression<Func<EntityAsString, bool>>>(lambdaExpression);
}

Expression<Func<EntityAsString, bool>> translatedExpression = x => x.Value == "Value2";

var mappedExpressionDelegate = mappedExpression.Compile();
var translatedExpressionDelegate = translatedExpression.Compile();

var entity = new EntityAsString { Value = SimpleEnum.Value2.ToString() };
var mappedResult = mappedExpressionDelegate(entity);
var translatedResult = translatedExpressionDelegate(entity);

Assert.True(translatedResult);
Assert.Equal(mappedResult, translatedResult);
}

[Fact]
public void BinaryExpressionOfCoalescedEnumToInt()
{
Expression<Func<EntityAsInt, bool>> mappedExpression;
{
var param = Expression.Parameter(typeof(EntityDto), "x");
var property = Expression.Property(param, nameof(EntityDto.Value));
var usedConstant = Expression.Constant(SimpleEnum.Value2, typeof(SimpleEnum?));
var otherConstant = Expression.Constant(SimpleEnum.Value1, typeof(SimpleEnum));
var coalesce = Expression.Coalesce(usedConstant, otherConstant);
var binaryExpression = Expression.Equal(property, coalesce);
var lambdaExpression = Expression.Lambda(binaryExpression, param);
mappedExpression = Mapper.Map<Expression<Func<EntityAsInt, bool>>>(lambdaExpression);
}

Expression<Func<EntityAsInt, bool>> translatedExpression = translatedExpression = x => x.Value == ((int?)2 ?? 1);

var mappedExpressionDelegate = mappedExpression.Compile();
var translatedExpressionDelegate = translatedExpression.Compile();

var entity = new EntityAsInt { Value = (int)SimpleEnum.Value2 };
var mappedResult = mappedExpressionDelegate(entity);
var translatedResult = translatedExpressionDelegate(entity);

Assert.True(translatedResult);
Assert.Equal(mappedResult, translatedResult);
}
}
}