I am using Automapper to patch an object onto itself.
I want to ignore null
values from the source object when doing the mapping. This works for collections by using AllowNullCollections = true;
. When I have a destination object that contains a dictionary with some elements and I try to map a source object that contains a null dictionary, I expect the null dictionary to be ignored because I am ignoring null collections.
But on my destination object, the Dictionary wind up empty. Is this the expected behavior for Dictionaries?
This is my profile
AllowNullCollections = true;
CreateMap<T, T>()
.ForAllOtherMembers(o => o.Condition((s, d, value) => value != null));
And my mapping call
var context = PatcherProfileContext.Create();
var originalEntity = new TestEntity
{
Dictionary = new Dictionary<string, object> { { "key", "value" } }
};
var patchEntity = new TestEntity
{
Dictionary = null
};
context.Mapper.Map(patchEntity, originalEntity);
originalEntity.Dictionary.ShouldHaveSingleItem();
But It winds up being empty.