MapsterMapper / Mapster

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

Incorrect mapping of an ignored property #458

Open PopMal opened 2 years ago

PopMal commented 2 years ago

Mapster Setting:

TypeAdapterConfig.GlobalSettings.Compiler = exp => exp.CompileWithDebugInfo(); TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true); TypeAdapterConfig.GlobalSettings.RequireDestinationMemberSource = true; TypeAdapterConfig.GlobalSettings.Scan(typeof(MappingConfig).Assembly); TypeAdapterConfig.GlobalSettings.Compile();

Entity Configuration:

TypeAdapterConfig<Entity, Dto>.NewConfig() .Map(dto => dto .Id, entity=> entity.Id) .Ignore(dto => dto .PropToIgnore)

My entity (PropToIgnore):

public class PropToIgnore { private PropToIgnore() { } public PropToIgnore(string name, string desc) { Desc = desc; Name = name; } public string Name { get; private set; } public string Desc{ get; private set; } }

Runtime error:

"System.TypeInitializationException: The type initializer for 'Mapster.TypeAdapter2' threw an exception.\n ---> Mapster.CompileException: Error while compiling\nsource=Models.PropToIgnore\ndestination=Models.PropToIgnore\ntype=Map\n ---> System.InvalidOperationException: No default constructor for type 'PropToIgnore', please use 'ConstructUsing' or 'MapWith'\n at Mapster.Adapters.BaseAdapter.CreateInstantiationExpression(Expression source, Expression destination, CompileArgument arg)\n at Mapster.Adapters.ClassAdapter.CreateInstantiationExpression(Expression source, Expression destination, CompileArgument arg)\n at Mapster.Adapters.BaseAdapter.CreateBlockExpressionBody(Expression source, Expression destination, CompileArgument arg)\n at Mapster.Adapters.BaseAdapter.CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)\n at Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgument arg)\n at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)\n --- End of inner exception stack trace ---\n at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)\n at Mapster.TypeAdapterConfig.CreateMapExpression(TypeTuple tuple, MapType mapType)\n at Mapster.TypeAdapterConfig.<GetMapFunction>b__58_0(TypeTuple tuple)\n at Mapster.TypeAdapterConfig.<>c__DisplayClass55_01.b__0(TypeTuple types)\n at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)\n at Mapster.TypeAdapterConfig.AddToHash[T](ConcurrentDictionary2 hash, TypeTuple key, Func2 func)\n at Mapster.TypeAdapterConfig.GetMapFunction(Type sourceType, Type destinationType)\n at Mapster.TypeAdapterConfig.GetMapFunction[TSource,TDestination]()\n at Mapster.TypeAdapter`2..cctor()\n --- End of inner exception stack trace

Question:: Why does an ignored property participate in mapping at all? And how do you fix all of this so you don't have to configure an ignored entity? Although changing the empty private constructor to a public one removes the error, it does not seem logical, because it is in the mapster's ignorance.

Addition

I forgot to add that locally everything works out without errors, but as soon as you pour on the prod, then an error comes out on top. And I also noticed that if you use map(entity => entity.dPropClass, dto => dto.dPropClass, (e) => false) instead of the ignor operator, the error pops up locally. Does this mean that the ignore statement unfolds in the map under the hood?

DocSvartz commented 1 year ago

Hello, You create New instance or update ? example:

_Entity.Adapt() or _Entity.Adapt(_Dto)