Skip to content

Commit 18c37f1

Browse files
authored
Error in #161 fix. (#167)
* Error in #161 fix. * Failing test
1 parent 8266800 commit 18c37f1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/AutoMapper.Extensions.ExpressionMapping/XpressionMapperVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private bool ShouldConvertMemberExpression(Type initialType, Type mappedType)
152152
initialType = Nullable.GetUnderlyingType(initialType);
153153

154154
if (mappedType.IsNullableType())
155-
initialType = Nullable.GetUnderlyingType(mappedType);
155+
mappedType = Nullable.GetUnderlyingType(mappedType);
156156

157157
return mappedType == Enum.GetUnderlyingType(initialType);
158158
}

tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/ExpressionMappingEnumToNumericOrString.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ protected override MapperConfiguration Configuration
8787
.ReverseMap();
8888
config.CreateMap<Entity<int>, EntityDto<SimpleEnumInt>>()
8989
.ForMember(dest => dest.Value, config => config.MapFrom(src => src.Value))
90+
.ReverseMap();
91+
config.CreateMap<Entity<int?>, EntityDto<SimpleEnumInt>>()
92+
.ForMember(dest => dest.Value, config => config.MapFrom(src => src.Value))
9093
.ReverseMap();
9194
config.CreateMap<Entity<uint>, EntityDto<SimpleEnumUInt>>()
9295
.ForMember(dest => dest.Value, config => config.MapFrom(src => src.Value))
@@ -198,6 +201,29 @@ public void BinaryExpressionEquals<TEnum, TNumeric>(TEnum enumConstant, TNumeric
198201
Assert.Equal(result, correctResult);
199202
}
200203

204+
[Fact]
205+
public void BinaryExpressionEqualsWithNullable()
206+
{
207+
SimpleEnumInt enumConstant = SimpleEnumInt.Value2;
208+
int? numericConstant = 2;
209+
Expression<Func<Entity<int?>, bool>> mappedExpression;
210+
{
211+
var param = Expression.Parameter(typeof(EntityDto<SimpleEnumInt>), "x");
212+
var property = Expression.Property(param, nameof(EntityDto<SimpleEnumInt>.Value));
213+
var constantExp = Expression.Constant(enumConstant, typeof(SimpleEnumInt));
214+
var binaryExpression = Expression.Equal(property, constantExp);
215+
var lambdaExpression = Expression.Lambda(binaryExpression, param);
216+
mappedExpression = Mapper.Map<Expression<Func<Entity<int?>, bool>>>(lambdaExpression);
217+
}
218+
219+
var mappedExpressionDelegate = mappedExpression.Compile();
220+
221+
var entity = new Entity<int?> { Value = numericConstant };
222+
var result = mappedExpressionDelegate(entity);
223+
224+
Assert.True(result);
225+
}
226+
201227
private class ComplexEntity
202228
{
203229
public int intToEnum { get; set; }

0 commit comments

Comments
 (0)