Skip to content
Closed
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
20 changes: 20 additions & 0 deletions src/Mapster.Tests/WhenPerformingDestinationTransforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ public void Adapter_Destination_Transform_CreateNewIfNull()
destination.Set.Count.ShouldBe(0);
}

[TestMethod]
public void Explicit_Null_Mapping_Is_Not_Overridden_By_EmptyCollectionIfNull()
{
var config = new TypeAdapterConfig();
config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);
config.NewConfig<ExplicitNullSource, ExplicitNullDestination>()
.Map(d => d.Strings, _ => (string[]?)null);

var destination = new ExplicitNullSource([]).Adapt<ExplicitNullDestination>(config);

destination.Strings.ShouldBeNull();
}

#region TestClasses

public class SimplePoco
Expand Down Expand Up @@ -145,6 +158,13 @@ public class CollectionDto
public ISet<string> Set { get; set; }
}

public record ExplicitNullSource(string?[] Strings);

public class ExplicitNullDestination
{
public string[]? Strings { get; set; }
}

#endregion

}
Expand Down
13 changes: 12 additions & 1 deletion src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp
: CreateAdaptExpressionCore(_source, destinationType, arg, mapping, destination);

//transform(adapt(_source));
if (notUsingDestinationValue)
if (notUsingDestinationValue && !HasExplicitMemberMap(mapping, arg))
{
var transform = arg.Settings.DestinationTransforms.Find(it => it.Condition(exp.Type));
if (transform != null)
Expand All @@ -521,5 +521,16 @@ internal Expression CreateAdaptExpression(Expression source, Type destinationTyp

return exp.To(destinationType);
}

static bool HasExplicitMemberMap(MemberMapping? mapping, CompileArgument arg)
{
if (mapping?.DestinationMember == null)
return false;

var memberName = mapping.DestinationMember.Name;
return arg.Settings.Resolvers.Any(resolver =>
!resolver.IsChildPath &&
resolver.DestinationMemberName.Equals(memberName, StringComparison.InvariantCultureIgnoreCase));
}
}
}