Automapper: Passing NULL objects to mapper.Map<T>() raises exception

Created on 24 May 2016  路  7Comments  路  Source: AutoMapper/AutoMapper

Hi, passing NULL object to mapper.map now causes NullReference exception. I'm pretty sure I was getting null in previous versions and this is the behavior I'd like to get now :) Is there any workaround?

       [TestMethod]

        public void MappingTest()

        {

            var config = new MapperConfiguration(cfg =>

            {

                cfg.AllowNullDestinationValues = true;

                cfg.CreateMap<GetCustomersRequest, GetCustomersFitler>();

            });

            IMapper mapper = config.CreateMapper();

            var filter = mapper.Map<GetCustomersFitler>(null);
        }
Bug

Most helpful comment

As weird as this behavior is, I can put it back since it is breaking.

All 7 comments

You can call

Map<Source, Destination>(null)

So what do you want it to do? Return null? That's inconsistent with the rest of AutoMapper, that if you pass in null, you at least get a blank object in return.

jbogard
Well, latest stable version 4.2.1 returns NULL if NULL has been passed to Map method, and this is the behavior I'm used to.
lbargaoanu
My bad, I referenced 5.0 which is beta i suppose.

i probably intentionally broke this behavior. "why would anyone pass null in, it makes no sense because i can't infer the map to use"

You obviously can't do Mapper.Map(null), because you can't get type of null. Even Visual Studio IDE won't allow you to do so. But in case of generic method where you specify type explicitly returning null makes much more sense. Why would null of one type be equivalent to an instance of the other type? Null is null, no object.

As weird as this behavior is, I can put it back since it is breaking.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings