Skip to content

Commit 7128768

Browse files
committed
Reinstating object creation checks before calling object-created callbacks, test coverage for fix
1 parent 7a65b02 commit 7128768

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringObjectCreationCallbacks.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,5 +446,29 @@ public void ShouldUseATypedParentContextAccessForACallbackArgument()
446446
mapper.Map(source).OnTo(target);
447447
}
448448
}
449+
450+
[Fact]
451+
public void ShouldOnlyInvokeACallbackWhenAnObjectIsCreated()
452+
{
453+
using (var mapper = Mapper.CreateNew())
454+
{
455+
var createdInstance = default(object);
456+
457+
mapper.After
458+
.CreatingInstances
459+
.Call(ctx => createdInstance = ctx.CreatedObject);
460+
461+
var source = new PublicField<Product> { Value = new Product { ProductId = "YEAH" } };
462+
var target = new PublicField<Product> { Value = new Product() };
463+
mapper.Map(source).OnTo(target);
464+
465+
createdInstance.ShouldBeNull();
466+
467+
target.Value = null;
468+
var result = mapper.Map(source).OnTo(target);
469+
470+
createdInstance.ShouldBe(result.Value);
471+
}
472+
}
449473
}
450474
}

AgileMapper/Configuration/UserConfiguredItemBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ protected bool SourceAndTargetTypesAreTheSame(UserConfiguredItemBase otherConfig
6767
ConfigInfo.HasSameTargetTypeAs(otherConfiguredItem.ConfigInfo);
6868
}
6969

70-
public virtual Expression GetConditionOrNull(IMemberMapperData mapperData)
70+
public Expression GetConditionOrNull(IMemberMapperData mapperData)
7171
=> GetConditionOrNull(mapperData, CallbackPosition.After);
7272

73-
protected Expression GetConditionOrNull(IMemberMapperData mapperData, CallbackPosition position)
73+
protected virtual Expression GetConditionOrNull(IMemberMapperData mapperData, CallbackPosition position)
7474
=> ConfigInfo.GetConditionOrNull(mapperData, position, TargetMember);
7575

7676
public virtual bool AppliesTo(IBasicMapperData mapperData)

AgileMapper/ObjectPopulation/ObjectCreationCallbackFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public override bool AppliesTo(CallbackPosition callbackPosition, IBasicMapperDa
2828

2929
protected override bool TargetMembersMatch(IBasicMapperData mapperData) => true;
3030

31-
public override Expression GetConditionOrNull(IMemberMapperData mapperData)
31+
protected override Expression GetConditionOrNull(IMemberMapperData mapperData, CallbackPosition position)
3232
{
33-
var condition = GetConditionOrNull(mapperData, CallbackPosition);
33+
var condition = base.GetConditionOrNull(mapperData, position);
3434

3535
if (CallbackPosition != CallbackPosition.After)
3636
{

0 commit comments

Comments
 (0)