Skip to content

Fix MapWith and MapToTargetWith using applySettings: true when mapping to a primitive type #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 11 additions & 30 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,6 @@ protected virtual Expression CreateExpressionBody(Expression source, Expression?
if (CheckExplicitMapping && arg.Context.Config.RequireExplicitMapping && !arg.ExplicitMapping)
throw new InvalidOperationException("Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists");

#region CustomMappingPrimitiveImplimentation

if (arg.Settings.MapToTargetPrimitive == true)
{
Expression dest;

if (destination == null)
{
dest = arg.DestinationType.CreateDefault();
}
else
dest = destination;

var customConvert = arg.Context.Config.CreateMapToTargetInvokeExpressionBody(source.Type, arg.DestinationType, source, dest);

arg.MapType = MapType.MapToTarget;
return customConvert;
}

if (arg.Settings.MapWithToPrimitive == true)
{
return arg.Context.Config.CreateMapInvokeExpressionBody(source.Type, arg.DestinationType, source);
}

#endregion CustomMappingPrimitiveImplimentation

var oldMaxDepth = arg.Context.MaxDepth;
var oldDepth = arg.Context.Depth;
try
Expand All @@ -128,11 +102,18 @@ protected virtual Expression CreateExpressionBody(Expression source, Expression?
arg.Context.Depth++;
}

if (CanInline(source, destination, arg))
if(arg.Settings.MapToTargetPrimitive == true)
{
// skip inline mapping
}
else
{
var exp = CreateInlineExpressionBody(source, arg);
if (exp != null)
return exp.To(arg.DestinationType, true);
if (CanInline(source, destination, arg))
{
var exp = CreateInlineExpressionBody(source, arg);
if (exp != null)
return exp.To(arg.DestinationType, true);
}
}

if (arg.Context.Running.Count > 1 &&
Expand Down
5 changes: 0 additions & 5 deletions src/Mapster/TypeAdapterSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -612,11 +612,6 @@ public TypeAdapterSetter<TSource, TDestination> MapWith(Expression<Func<TSource,
{
this.CheckCompiled();

if(typeof(TSource).IsMapsterPrimitive() || typeof(TDestination).IsMapsterPrimitive())
{
this.Settings.MapWithToPrimitive = true;
}

if (applySettings)
{
var adapter = new DelegateAdapter(converterFactory);
Expand Down
6 changes: 0 additions & 6 deletions src/Mapster/TypeAdapterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ public object? GenerateMapper
set => Set(nameof(GenerateMapper), value);
}

public bool? MapWithToPrimitive
{
get => Get(nameof(MapWithToPrimitive));
set => Set(nameof(MapWithToPrimitive), value);
}

/// <summary>
/// Not implemented
/// </summary>
Expand Down
Loading