Open mishael-o opened 2 years ago
I'm seeing this as well. Version 7.2 .Net Framework 4.8
using Mapster;
using Xunit;
namespace AdaptTests
{
public class EnumMappingTests
{
[Fact]
public void Doesnt_Work_Per_Type()
{
TypeAdapterConfig.GlobalSettings.Default
.EnumMappingStrategy(EnumMappingStrategy.ByValue);
TypeAdapterConfig<MyClassOne, MyClassTwo>
.NewConfig()
.Map(two => two.That, one => one.This)
.EnumMappingStrategy(EnumMappingStrategy.ByName);
TestEnumAdapt();
}
[Fact]
public void Works_When_Setting_Globally()
{
TypeAdapterConfig.GlobalSettings.Default
.EnumMappingStrategy(EnumMappingStrategy.ByName);
TypeAdapterConfig<MyClassOne, MyClassTwo>
.NewConfig()
.Map(two => two.That, one => one.This);
TestEnumAdapt();
}
private static void TestEnumAdapt()
{
TypeAdapterConfig.GlobalSettings.RequireExplicitMapping = true;
var myClassOne = new MyClassOne
{
This = OneThing.TheMatchingOne
};
var myClassTwo = myClassOne.Adapt<MyClassTwo>();
Assert.Equal(TwoThing.TheMatchingOne, myClassTwo.That);
}
public class MyClassOne
{
public OneThing This { get; set; }
}
public class MyClassTwo
{
public TwoThing That { get; set; }
}
}
public enum TwoThing
{
TheMatchingOne = 1000,
Nah = 2000,
Rah = 3000
}
public enum OneThing
{
Rah = 0,
TheMatchingOne = 21,
Nah = 200
}
}
bug? i have same question
nobody?
EnumMappingStrategy
works correctly. The problem is, that you cannot set it on configuration for those classes. It does not make sense, because there may be many different enum properties there and probably you do not want to map all of them by name. You have to set EnumMappingStrategy
for enum types you want it to work for (and probably you want to set it for both directions). So the correct settings is:
TypeAdapterConfig<OneThing, TwoThing>
.NewConfig()
.EnumMappingStrategy(EnumMappingStrategy.ByName);
TypeAdapterConfig<TwoThing, OneThing>
.NewConfig()
.EnumMappingStrategy(EnumMappingStrategy.ByName);
@andrerav EnumAdapter child of PrimitiveAdapter and also do not support Custom Mapping. This is not a bug but rather a feature request @satano you're right. You can definitely install it. But it will only work when Mapping will occur from Enum to Enum for which this setting is set.
Hi, I have tried to configure EnumMappingStrategy on individual mapping via the static and instance objects but it has no effect on the mapping.
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig().EnumMappingStrategy(EnumMappingStrategy.ByName);
config.NewConfig<SimplePoco, SimpleDto>().EnumMappingStrategy(EnumMappingStrategy.ByName)
However it works when I set it globally, which is not the desired config I want. Is there something I am missing or is this a bug?