Skip to content

ReverseMap creates invalid configuration when Source and Destination support different int values #27

@sjbuck

Description

@sjbuck

Source/destination types

public enum Source { Default, OnlyInSource = 3 }
public enum Destination { Default, OnlyInDestination = 4}

Mapping configuration

cfg.CreateMap<Source, Destination>()
    .ConvertUsingEnumMapping(opt => opt.MapByValue()
        .MapValue(Source.OnlyInSource, Destination.OnlyInDestination))
    .ReverseMap();

Version: 3.2.0

Expected behavior

  • AssertConfigurationIsValid() doesn't throw
  • Destination.OnlyInDestination will map to Source.OnlyInSource because of the ReverseMap()

Actual behavior

  • AssertConfigurationIsValid() throws
  • Trying to map Destination to Source throws

In both cases the error is:

AutoMapper.AutoMapperMappingException : Value OnlyInDestination of type AutoMapper.Extensions.EnumMapping.Tests.ReverseCustomEnumValueMappingByValue+OnlyInSourceValueIsMappedToOnlyInDestiationValue+Destination not supported

Steps to reproduce

// This slightly altered test from the project's test suite will fail
public class OnlyInSourceValueIsMappedToOnlyInDestinationValue : AutoMapperSpecBase
{
    readonly List<Source> _results = new List<Source>();

    public enum Source { Default, OnlyInSource = 3 }
    public enum Destination { Default, OnlyInDestination = 4}

    protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
    {
        cfg.EnableEnumMappingValidation();
        cfg.CreateMap<Source, Destination>()
            .ConvertUsingEnumMapping(opt => opt.MapByValue()
                .MapValue(Source.OnlyInSource, Destination.OnlyInDestination))
            .ReverseMap();
    });
    
    protected override void Because_of()
    {
        _results.Add(Mapper.Map<Destination, Source>(Destination.Default));
        _results.Add(Mapper.Map<Destination, Source>(Destination.OnlyInDestination));
    }

    [Fact]
    public void Should_map_using_custom_map()
    {
        _results[0].ShouldBe(Source.Default);
        _results[1].ShouldBe(Source.OnlyInSource);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions