Nullable extension method resolver #3899
-
https://gist.github.com/iamzhaoxu/533a047bec2a469e32b5c786b7d1fd3d Source/destination types public enum YesNoType { Y,N }
public class Source {
public class Inner {
public bool? IsFlag {get;set;}
}
}
public class Desitination{
public YesNoType IsFlag {get;set;}
}
Mapping configurationCreateMap<Source , Desitination>()
.ForMember(d => d.Fragile, o => o.MapFrom(s => s.Inner.IsFlag == true ? YesNoType.Y : YesNoType.N)) Version: x.y.z11.0.1 Expected behaviorWhen Source.Inner.IsFlag is null value, I will expect the mapping will be triggered and according to the mapping logic, the null value should be converted to YesNoType.N. I expected Desitination.IsFlag value should be YesNoType.N Actual behaviorDesitination.IsFlag is YesNoType.Y which looks like it pick the default value from the YesNoType Enum. The expected behaviour is working with version 10.1.1 but not with version 11.0.1. I have a unit test to verify it with upgrading/downgrading the automapper version. Steps to reproducevar source = new Source { new Inner { IsFlag = null }};
var dest = _mapper.Map<Desintation>(source);
Assert.AreEqual(dest.IsFlag , YesNoType.N); // This one will fail since the value of dest.IsFlag is YesNoType.Y |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
cfg.CreateMap<Model, Dto>().ForMember(d => d.IsFlag, o => o.MapFrom(s => s.Inner.IsFlag));
cfg.CreateMap<bool?, YesNo>().ConvertUsing(s=>s.ConvertIsFlag()); Or you can use a |
Beta Was this translation helpful? Give feedback.
Or you can use a
Func
based resolver, which is not null checked. What happens here is that the new null checking code doesn't call your extension method becauseIsFlag
isnull
. That seems reasonable.