Open generic and polymorphic mapping. Breaking change between versions 3.3.1 and 4.0.0 and greater

41 views
Skip to first unread message

Leo

unread,
Dec 10, 2015, 2:52:45 PM12/10/15
to AutoMapper-users
It appears to be a breaking change in how polymorphic collections mapped between versions 3.3.1 and 4.0.0 and greater.

I'm attempting to map the following list:
var entities = new List<BaseEntity>
{
    new ChildEntity<int> {Id = 0, ValueOne = 1, ValueTwo = 2},
    new ChildEntity<string> {Id = 0, ValueOne = "One", ValueTwo = "Two"}
};

var dtos = Mapper.Map<List<BaseDto>>(entities);

The mapping I'm using is:
Mapper.CreateMap<BaseEntity, BaseDto>()
    .Include(typeof(ChildEntity<>), typeof(ChildDto<>));

Mapper.CreateMap<ChildEntity<int>, ChildDto<int>>();
Mapper.CreateMap<ChildEntity<string>, ChildDto<string>>();


Version 3.3.1 correctly maps a polymorphic entity collection to a polymorphic DTO collection:
Assert.IsType(typeof(ChildDto<int>), dtos[0]);
Assert.IsType(typeof(ChildDto<string>), dtos[1]);

Version 4 maps to a collection of base DTOs:
Assert.IsType(typeof(ChildDto<int>), dtos[0]); // Fails
Assert.IsType(typeof(BaseDto), dtos[0]); //Succeeds

Is it by design? How the correct mapping would look like?

Here is the gist:

https://gist.github.com/anonymous/871fa2410d242436fe4c


Jimmy Bogard

unread,
Dec 14, 2015, 9:17:35 AM12/14/15
to automapper-users
Can you use Initialize instead of CreateMap here?

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages