Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Mapping between two enums happening by value instead of using configured custom mapping

Open agoodsell5 opened this issue 4 years ago • 2 comments

Issue

I have need to map between 2 different enums that are on 2 different classes. I have defined config for mapping between these 2 enums as they have both different values and different names. These configured mappings work as expected. However, when I map between the two parent classes, the enums are mapped by value rather than by using the configured mapping.

Example

Here we have 2 classes each with an enum.

public class Class1
{
  public Enum1 TheFirstEnum;
}
public class Class2
{
  public Enum2 TheSecondEnum;
}

Config is defined like this for mapping between two enums where they have simple methods with switch statements for mapping from one to another:

this.NewConfig<Enum1, Enum2>()
                .MapWith(enum1Value => this.MapToEnum2Value(enum1Value));

this.NewConfig<Enum2, Enum1>()
                .MapWith(enum2Value => this.MapToEnum1Value(enum2Value));

The above config works great. If I call enum1.Adapt<Enum2>() it works perfectly.

The following config is then added:

this.NewConfig<Class1, Class2>()
                .Map(class2 => class2.TheSecondEnum, class1 => class1.TheFIrstEnum);

If I then call class1.Adapt<Class2> the enum field tries to map using value instead of my custom mapping defined above.

If I instead write the config like this:

this.NewConfig<Class1, Class2>()
               .MapWith(class1Value => new Class2 { TheSecondEnum = class1Value.TheFirstEnum.Adapt<Enum2>(this) });

Then it works.

I would expect my preferred sytanx to automatically pick up that there is a config for mapping between these 2 types rather than simply defaulting to mapping by value.

Is there a config setting I am missing somewhere that would mean it would look for a configred mapping for mapping between enums before defaulting to mapping by value?

agoodsell5 avatar Aug 20 '21 11:08 agoodsell5

Experience the same issue, any advises ?

jvmlet avatar Feb 14 '23 13:02 jvmlet

Looks like fixed in 7.4.0

jvmlet avatar Feb 12 '24 08:02 jvmlet