MapsterMapper / Mapster

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

Mapster.Tool generates incorrect mapping extension method names when a destination type is nullable or generic #580

Closed rafalka closed 1 year ago

rafalka commented 1 year ago

Mapster.Tool generates incorrect mapping extension method names when a destination type is nullable or generic

Example: Declaration:

    public class GenericTestSrc
    {
        public int Val { get; set; }
    }
    public class GenericTestDst<T>
    {
        public T Val { get; set; }
    }

    public record MyDateTime(DateTime? DateTime);

Mapper config:


            config.ForType<MyDateTime, DateTime?>()
                .MapWith(s => s.DateTime.Value )
                .GenerateMapper(MapType.Map);

            config.ForType<GenericTestSrc, GenericTestDst<int>>()
                .GenerateMapper(MapType.Map);

What gets generated:

        public static System.DateTime? AdaptToDateTime?(this MapLib.MapTest.MyDateTime s)
        {
            return (System.DateTime?)s.DateTime.Value;
        }

        public static MapLib.MapTest.GenericTestDst<int> AdaptToGenericTestDst<int>(this MapLib.MapTest.GenericTestSrc p1)
        {
            return p1 == null ? null : new MapLib.MapTest.GenericTestDst<int>() {Val = p1.Val};
        }

Attached sample project: MapsterBug.zip