Skip to content

Commit a8d3f09

Browse files
committed
add RequireExplicitMappingPrimitive config
1 parent 155a723 commit a8d3f09

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/Mapster/Adapters/PrimitiveAdapter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ protected override bool CanMap(PreCompileArgument arg)
1818

1919
protected override Expression CreateExpressionBody(Expression source, Expression? destination, CompileArgument arg)
2020
{
21+
if (arg.SourceType != arg.DestinationType && arg.Context.Config.RequireExplicitMappingPrimitive && !arg.ExplicitMapping)
22+
throw new InvalidOperationException("Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists");
2123

2224
if (arg.Settings.MapToTargetPrimitive == true)
2325
{

src/Mapster/TypeAdapterConfig.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ private static List<TypeAdapterRule> CreateRuleTemplate()
8282

8383
public bool RequireDestinationMemberSource { get; set; }
8484
public bool RequireExplicitMapping { get; set; }
85+
public bool RequireExplicitMappingPrimitive { get; set; }
8586
public bool AllowImplicitDestinationInheritance { get; set; }
8687
public bool AllowImplicitSourceInheritance { get; set; } = true;
8788
public bool SelfContainedCodeGeneration { get; set; }
@@ -495,7 +496,7 @@ private LambdaExpression CreateMapInvokeExpression(Type sourceType, Type destina
495496

496497
internal Expression CreateMapInvokeExpressionBody(Type sourceType, Type destinationType, Expression p)
497498
{
498-
if (RequireExplicitMapping)
499+
if (RequireExplicitMapping || RequireExplicitMappingPrimitive)
499500
{
500501
var key = new TypeTuple(sourceType, destinationType);
501502
_mapDict[key] = Compiler(CreateMapExpression(key, MapType.Map));
@@ -518,7 +519,7 @@ internal Expression CreateMapInvokeExpressionBody(Type sourceType, Type destinat
518519

519520
internal Expression CreateMapToTargetInvokeExpressionBody(Type sourceType, Type destinationType, Expression p1, Expression p2)
520521
{
521-
if (RequireExplicitMapping)
522+
if (RequireExplicitMapping || RequireExplicitMappingPrimitive)
522523
{
523524
var key = new TypeTuple(sourceType, destinationType);
524525
_mapToTargetDict[key] = Compiler(CreateMapExpression(key, MapType.MapToTarget));

0 commit comments

Comments
 (0)