MapsterMapper / Mapster

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

Upgrade to Mapster 3.1.8 causing issue #133

Closed tjs-shah closed 6 years ago

tjs-shah commented 6 years ago

We are trying to upgrade from MapsterMapper 3.1.6 to 3.1.8 and getting below error at runtime. Our project is using MapsterMapper 3.1.6 in production code base and everything seems to be fine. Now runtime issues have been identified. Its only after the upgrade in development environment we are getting issue.

Error is as below:

System.ArgumentException occurred

Message=Only first level member access on destination allowed (eg. dest => dest.Name) Parameter name: member Source=Mapster StackTrace: at Mapster.ReflectionUtils.GetMemberInfo(Expression member, Boolean source) at Mapster.TypeAdapterSetter2.Map[TDestinationMember,TSourceMember](Expression1 member, Expression1 source, Expression1 shouldMap)

chaowlert commented 6 years ago

Could you pls post your type adapter configuration which might cause problem?

I found out that, if we input cascade property mapping on destination side like .Map(dest => dest.ZipCode.Zip,, it will cause incorrect mapping. That's why I throw exception. There might be some corner case that I should handle.

tjs-shah commented 6 years ago

In .NET Core startup configuration we have following setup:

var iregisterType = typeof(IRegister); var iregisterTypes = from assembly in AppDomain.CurrentDomain.GetAssemblies() from assemblyType in assembly.GetTypes() where assemblyType.GetInterfaces().Contains(iregisterType) select assembly;

        TypeAdapterConfig.GlobalSettings.Scan(iregisterTypes.Distinct().ToArray());
        TypeAdapterConfig.GlobalSettings.Default.AddDestinationTransform((string str) => str.Trim());
        TypeAdapterConfig.GlobalSettings.Default.ShallowCopyForSameType(true);
        TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true);

        services.AddTransient<IAdapter>(instance => TypeAdapter.GetInstance());

        return services;

In Mapping we have as follows: public class IndustrySectorMapping : IRegister { public void Register(TypeAdapterConfig config) { config.NewConfig<GetIndustrySector_Result, IndustrySector>() .Map(dest => dest.SectorId.Value, src => src.SectorId); } }

where GetIndustrySector_Result SectorId is null int.

This was working in 3.1.6. Let me know if you need more information.

chaowlert commented 6 years ago

The problem is in .Map(dest => dest.SectorId.Value, src => src.SectorId). I wonder if this code work in 3.1.6, Mapster should ignore this mapping, that's why I throw error in 3.1.7.

Is sectorId in destination is nullable? Could you pls change to .Map(dest => dest.SectorId, src => src.SectorId) (without .Value).

tjs-shah commented 6 years ago

Thanks for the help that worked with MapsterMapper 3.1.8. I verified and above mapping (with .Value) is working fine in our Production with MapsterMapper 3.1.6.

What I conclude is with "null able" type you just map property rather than .Value