MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.3k stars 328 forks source link

The IgnoreNullValues not working when the map function is declared #523

Open nsnail opened 1 year ago

nsnail commented 1 year ago

In the following example, the IgnoreNullValues loses its role. Although Person Name is a null, it still calls the trim method as defined in the map function, Leading in abnormal NullReferenceException

class Person1 { public string Name { get; set; } }

class Person2 { public string Name { get; set; }

}

void Main() { var mapper = new Mapper(); mapper.Config.NewConfig<Person1, Person2>().IgnoreNullValues(true) .Map(dest => dest.Name, src => src.Name.Trim()); // Although name is a null , name map to name.trim() still called ??? mapper.Map(new Person1()); }

nsnail commented 1 year ago

For the above reasons, I have to add it to each mapping rule :

.IgnoreIf((src, dest) => src.Depts == null, dest => dest.DeptIds) .Map(dest => dest.DeptIds, src => src.Depts.Select(x => x.Id))

I am looking for an easier way to implement it ,

However, the following code cannot meet the requirements :

TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true);