Skip to content

Commit 3ace993

Browse files
committed
cast the type map expression to the property type
1 parent 64b79b1 commit 3ace993

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/AutoMapper/Execution/ExpressionBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ public static Expression MapExpression(this IGlobalConfiguration configuration,
8787
{
8888
mapExpression = mapper.MapExpression(configuration, profileMap, memberMap, source, destination);
8989
nullCheck = mapExpression != source;
90-
mapExpression = ToType(mapExpression, typePair.DestinationType);
9190
}
9291
else
9392
{
9493
nullCheck = true;
9594
}
9695
}
97-
mapExpression ??= ContextMap(typePair, source, destination, memberMap);
96+
mapExpression = mapExpression == null ? ContextMap(typePair, source, destination, memberMap) : ToType(mapExpression, typePair.DestinationType);
9897
return nullCheck ? configuration.NullCheckSource(profileMap, source, destination, mapExpression, memberMap) : mapExpression;
9998
}
10099
public static Expression NullCheckSource(this IGlobalConfiguration configuration, ProfileMap profileMap, Expression source, Expression destination,

src/UnitTests/TypeConverters.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
namespace AutoMapper.UnitTests.CustomMapping;
2-
2+
public class StringToEnumConverter : AutoMapperSpecBase
3+
{
4+
class Source
5+
{
6+
public string Enum { get; set; }
7+
}
8+
class Destination
9+
{
10+
public ConsoleColor Enum { get; set; }
11+
}
12+
protected override MapperConfiguration CreateConfiguration() => new(c =>
13+
{
14+
c.CreateMap<string, Enum>().ConvertUsing(s => ConsoleColor.DarkCyan);
15+
c.CreateMap<Source, Destination>();
16+
});
17+
[Fact]
18+
public void Should_work()
19+
{
20+
Map<ConsoleColor>("").ShouldBe(ConsoleColor.DarkCyan);
21+
Map<Destination>(new Source()).Enum.ShouldBe(ConsoleColor.DarkCyan);
22+
}
23+
}
324
public class NullableConverter : AutoMapperSpecBase
425
{
526
public enum GreekLetters

0 commit comments

Comments
 (0)