Skip to content

ForAllMembers expression Condition incorrectly passes Nullable<T> source member as T #3926

Description

@xnoreq

Source/destination types

    public class Destination
    {
        public bool Toggle { get; set; }
    }

    public class Source
    {
        public bool? Toggle { get; set; }
    }

Mapping configuration

    public class TestProfile : Profile
    {
        public TestProfile()
        {
            AllowNullCollections = true;
            AllowNullDestinationValues = true;

            CreateMap<Source, Destination>()
                .ForAllMembers(expression => expression.Condition((src, dst, srcMember) => srcMember != null));
        }
    }

Version: 11.0.1

11.0.1

Expected behavior

In the ForAllMembers condition the srcMember should be of type bool? (for the Source.Toggle property) and the condition should evaluate to false (given Toggle = null as in the example program below). The destination thing should therefore stay unmodified.

Actual behavior

The srcMember is of type bool, so the information that the input was null gets lost. The ForAllMembers condition evaluates to true changing the destination thingy.

Steps to reproduce

    public class Program
    {
        static void Main(string[] args)
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile<TestProfile>());
            var mapper = config.CreateMapper();

            var thing = new Destination()
            {
                Toggle = true
            };

            var update = new Source()
            {
                Toggle = null
            };

            Console.WriteLine(thing.Toggle); // True
            mapper.Map(update, thing);
            Console.WriteLine(thing.Toggle); // False
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions