TIL: AutoMapper Only Considers Simple Mappings When Validating Configurations

I originally posted this post on my blog. Oh boy! AutoMapper once again. Today I have CreateMovieRequest with a boolean property ICanWatchItWithKids that I want to map to a MPA rating. You know G, PG, PG-13...those ones. If I can watch it with kids, the property MPARating on the destination type should get "General." Anything else gets "Restricted." To my surprise, this test fails: using AutoMapper; namespace TestProject1; [TestClass] public class WhyAutoMapperWhy { public class CreateMovieRequest { public string Name { get; set; } public bool ICanWatchItWithKids { get; set; } } public class Movie { public string Name { get; set;} public MPARating Rating { get; set;} } public enum MPARating { // Sure, there are more. // But these two are enough. General, Restricted } [TestMethod] public void AutoMapperConfig_IsValid() { var mapperConfig = new MapperConfiguration(options => { options.CreateMap(MemberList.Source) .ForMember( dest => dest.Rating, opt => opt.MapFrom(src => src.ICanWatchItWithKids ? MPARating.General : MPARating.Restricted)); }); mapperConfig.AssertConfigurationIsValid(); //

Mar 17, 2025 - 06:15
 0
TIL: AutoMapper Only Considers Simple Mappings When Validating Configurations

I originally posted this post on my blog.

Oh boy! AutoMapper once again.

Today I have CreateMovieRequest with a boolean property ICanWatchItWithKids that I want to map to a MPA rating. You know G, PG, PG-13...those ones.

If I can watch it with kids, the property MPARating on the destination type should get "General." Anything else gets "Restricted."

To my surprise, this test fails:

using AutoMapper;

namespace TestProject1;

[TestClass]
public class WhyAutoMapperWhy
{
    public class CreateMovieRequest
    {
        public string Name { get; set; }
        public bool ICanWatchItWithKids { get; set; }
    }

    public class Movie
    {
        public string Name { get; set;}
        public MPARating Rating { get; set;}
    }

    public enum MPARating
    {
        // Sure, there are more.
        // But these two are enough.
        General,
        Restricted
    }

    [TestMethod]
    public void AutoMapperConfig_IsValid()
    {
        var mapperConfig = new MapperConfiguration(options =>
        {
            options.CreateMap<CreateMovieRequest, Movie>(MemberList.Source)
                    .ForMember(
                        dest => dest.Rating,
                        opt => opt.MapFrom(src => src.ICanWatchItWithKids
                                                        ? MPARating.General
                                                        : MPARating.Restricted));
        });

        mapperConfig.AssertConfigurationIsValid();
        //