4

When using a custom type converter (ITypeConverter) with AutoMapper it does not seem to enter the type converters code if the source value is null, e.g:

Mapper.CreateMap<string, Enum>().ConvertUsing<EnumConverter>();

Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>("StringValue1"));
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null);
Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null);

The type converter looks something like:

public class EnumConvertor: ITypeConverter<string, Enum>
{
    public Enum Convert(ResolutionContext context)
    {
        string value = (string) context.SourceValue;

        switch (value)
        {
            case "StringValue2":
                return Enum.Value2;
            case "StringValue3":
                return Enum.Value3;
            case "StringValue1":
            default:
                return Enum.Value1;
        }
    }
}

In the last two cases, the results are:

Assert.AreEqual(Enum.Value1, Mapper.Map<Enum>(null);

0 - Not a valid enum value

Assert.AreEqual(Enum.Value1, Mapper.Map<Enum?>(null);

Null

From debugging into the tests, in these two cases the custom TypeConverter never gets hit and it seems AutoMapper has some initial checks in the mapper to map without resorting to the TypeConverter?

If I specify an empty string ("") the test works as expected.

Hux
  • 3,102
  • 1
  • 26
  • 33

1 Answers1

12

Taking a quick look at the source code of automapper, Mapper.Map(object source) does a null check. If source is null it will return the default value of T:

    public TDestination Map<TDestination>(object source, Action<IMappingOperationOptions> opts)
    {
        var mappedObject = default(TDestination);
        if (source != null)
        {
            var sourceType = source.GetType();
            var destinationType = typeof(TDestination);

            mappedObject = (TDestination)Map(source, sourceType, destinationType, opts);
        }
        return mappedObject;
    }
edhedges
  • 2,722
  • 2
  • 28
  • 61
James Hay
  • 12,580
  • 8
  • 44
  • 67
  • 3
    I too noticed this. The overload with `TSource` and `TDestination` does not have these checks, presumably because the Type is known to retrieve any mappings for that type, which cannot be determined if the source object is `null`. It does make sense, but is a little undefined. The correct test thus should be `Assert.AreEqual(Enum.Value1, Mapper.Map(null);` Which solves the problem. – Hux Mar 13 '14 at 13:38