-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Source and Destination Types
// Assume, as is the case for my use case, that both the source
// and destination enumerations are created via code generation
// from sources outside our control, and thus I cannot make updates
// or changes to them to work around the issue.
// This is idiomatic of how a Protobuf enum is generated, with the Unspecified
// value acting as a stand-in for when the field is not set by the client or server
public enum SourceEnum
{
Unspecified = 0,
Bar = 1,
Baz = 2,
}
// In our case, this is generated from an OpenAPI spec via Kiota
public enum DestinationEnum
{
BAR_ALT_NAME, // we can't map by name because the names don't match, even with case insensitivity turned on
BAZ_ALT_NAME,
}Mapping Configuration and Test Code
public class TestEnumProfile : Profile
{
public TestEnumProfile()
{
CreateMap<SourceEnum, DestinationEnum>()
.ConvertUsingEnumMapping(
opts =>
{
opts
.MapByValue()
.MapValue(SourceEnum.Bar, DestinationEnum.BAR_ALT_NAME)
.MapValue(SourceEnum.Baz, DestinationEnum.BAZ_ALT_NAME)
.MapException(SourceEnum.Unspecified, () => new InvalidOperationException($"Unspecified values are not supported"));
})
.ReverseMap();
}
}
public class Tests
{
IMapper _mapper = null!;
[SetUp]
public void Setup()
{
var config = new MapperConfiguration(configuration =>
{
configuration.EnableEnumMappingValidation();
configuration.AddMaps(typeof(Tests).GetTypeInfo().Assembly);
});
config.AssertConfigurationIsValid();
_mapper = config.CreateMapper();
}
[Test]
public void TestBarMapping()
{
// Passes
Assert.That(_mapper.Map<DestinationEnum>(SourceEnum.Bar), Is.EqualTo(DestinationEnum.BAR_ALT_NAME));
}
[Test]
public void TestBazMapping()
{
// Passes
Assert.That(_mapper.Map<DestinationEnum>(SourceEnum.Baz), Is.EqualTo(DestinationEnum.BAZ_ALT_NAME));
}
[Test]
public void TestUnspecifiedMapping()
{
// Passes
Assert.Throws<InvalidOperationException>(() =>
{
_mapper.Map<DestinationEnum>(SourceEnum.Unspecified);
});
}
[Test]
public void TestReverseBarMapping()
{
// Passes
Assert.That(_mapper.Map<SourceEnum>(DestinationEnum.BAR_ALT_NAME), Is.EqualTo(SourceEnum.Bar));
}
[Test]
public void TestReverseBazMapping()
{
// Failure: Expected: Baz But was: Bar
Assert.That(_mapper.Map<SourceEnum>(DestinationEnum.BAZ_ALT_NAME), Is.EqualTo(SourceEnum.Baz));
}
}Version: Automapper.Extensions.EnumMapping version 4.0.0
Expected Behavior
All tests pass.
Actual Behavior
All tests pass except for TestReverseBazMapping. The reverse mapping from DestinationEnum.BAZ_ALT_NAME to SourceEnum.Baz is incorrect - expected SourceEnum.Baz, but the mapper gives me SourceEnum.Bar instead.
Note that although the integer values of DestinationEnum.BAZ_ALT_NAME and SourceEnum.Baz are different, the other mappings in the other three tests also involve source and destination values that differ in integer value. This specific reverse mapping value is the one that produces an incorrect value, but not the others. Note also that all the values mapped in the Source -> Destination direction are mapped correctly, only some of the values in the reverse map are incorrect. I'm not clear on what about this particular value is triggering the failed map.