Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper null source value and custom type converter, fails to map?

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.

like image 640
Hux Avatar asked Dec 16 '22 00:12

Hux


1 Answers

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;
    }
like image 167
James Hay Avatar answered May 22 '23 03:05

James Hay