Skip to content
This repository

ForAllMembers incorrectly processing conditions #220

Closed
MichalLasak opened this Issue · 8 comments

3 participants

Michal Lasak Jimmy Bogard andycwk-tp
Michal Lasak

When using ForAllMembers with opt.Condition(src => !src.IsSourceValueNull), all values are incorrectly mapped to null, even those with specified values in source object.

Michal Lasak

Submitting unit test + possible workaround using custom condition.

 src/UnitTests/Bug/AllMembersNullSubstitute.cs |   48 +++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/src/UnitTests/Bug/AllMembersNullSubstitute.cs b/src/UnitTests/Bug/AllMembersNullSubstitute.cs
index 005b503..4cca42a 100644
--- a/src/UnitTests/Bug/AllMembersNullSubstitute.cs
+++ b/src/UnitTests/Bug/AllMembersNullSubstitute.cs
@@ -1,8 +1,10 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using AutoMapper;
 using AutoMapper.Mappers;
+using AutoMapper.UnitTests.ConditionalMapping;
 using Should;
 using NUnit.Framework;
 
@@ -42,5 +44,51 @@ public void Should_map_all_null_values_to_its_substitute()
            dest.Value2.ShouldEqual(string.Empty);
            dest.Value3.ShouldEqual(string.Empty);
        }
+
+        [Test]
+        public void Should_ignore_null_values()
+        {
+            Mapper.CreateMap()
+                .ForAllMembers(opt => opt.Condition(src => !src.IsSourceValueNull));
+
+            var source = new Source
+            {
+                Value1 = 5
+            };
+
+            var dest = Mapper.Map(source);
+
+            dest.Value1.ShouldEqual("5");
+            dest.Value2.ShouldEqual(null);
+            dest.Value3.ShouldEqual(null);
+        }
+
+        [Test]
+        public void Should_ignore_null_values_custom_condition()
+        {
+            Mapper.CreateMap()
+                .ForAllMembers(opt => opt.Condition(CustomCondition));
+
+            var source = new Source
+            {
+                Value1 = 5
+            };
+
+            var dest = Mapper.Map(source);
+
+            dest.Value1.ShouldEqual("5");
+            dest.Value2.ShouldEqual(null);
+            dest.Value3.ShouldEqual(null);
+        }
+
+        private static bool CustomCondition(ResolutionContext context)
+        {
+            var propertyInfo = context.Parent.SourceType.GetProperty(context.MemberName);
+            if (propertyInfo != null)
+            {
+                return propertyInfo.GetValue(context.Parent.SourceValue, null) != null;
+            }
+            return true;
+        }
    }
 }
Jimmy Bogard
Owner

So in this case the, the condition should evaluate against the source member?

Michal Lasak

I don't understand the question. Reported issue is probably related to #215 (see comment about populating ResolutionContext), https://groups.google.com/group/automapper-users/browse_thread/thread/64b81b54d29e67fc?pli=1 and #204. In my case, I wanted to map all non-null values from source object to destination object. Using custom condition, I created workaround and worked with populated parent resolution context.

Jimmy Bogard
Owner

Meant that IsSourceValueNull, you're wanting to evaluate against each member being mapped, not against the parent instance, right?

Michal Lasak

Yes, I want to evaluate against each member and map source value which is not null to destination member.

Jimmy Bogard
Owner

So, what should happen if evaluating the source or destination value results in an exception? Only asking because I tried adding this now, and a bunch of other tests failed assuming that conditions would short circuit before evaluating custom resolvers/mapping expressions

Michal Lasak

I assume if exception occurs, you should throw it. Just out of curiosity, what caused this functionality to stop working? Prior to 2.1 everything was fine and that unit test Should_ignore_null_values() would pass.

Jimmy Bogard jbogard closed this issue from a commit
Jimmy Bogard jbogard Moving the condition checking and context creation for full context f…
…or conditions, resolvers etc.

closes #220
closes #215
closes #204
6c38546
Jimmy Bogard jbogard closed this in 6c38546
andycwk-tp

Hi, When will you be updating the nuget package to include this fix?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.