Skip to content

Commit

Permalink
#135 Enum to same enum conversions
Browse files Browse the repository at this point in the history
When performing a conversion from an enum to the same enum, the
AnyToEnumConverter is triggered, which performs a full cycle conversion,
using the toString() method of source and the valueOf() of the target
enum.

When the source enum is the same as the target enum and the source enum
has an overridden toString() method, this fails because the value can
not be mapped to the enum.

Instead, the AnyToEnumConverter now checks whether the source is of the
same enum class as the target. If this is the case, the conversion task
is skipped and the source is returned literally.
  • Loading branch information
robert-bor committed Jul 17, 2021
1 parent 1e2ee76 commit 65a9628
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ protected Enum<?> doConvert(Object source, Class<? extends Enum<?>> targetClass)
if (source == null) {
return null;
}
if (targetClass.isInstance(source)) {
return (Enum<?>)source;
}
String sourceText = source.toString();
if (isNotEmpty(sourceText)) {
return valueOf(targetClass, sourceText);
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/io/beanmapper/BeanMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@
import io.beanmapper.testmodel.encapsulate.source_annotated.Car;
import io.beanmapper.testmodel.encapsulate.source_annotated.CarDriver;
import io.beanmapper.testmodel.encapsulate.source_annotated.Driver;
import io.beanmapper.testmodel.enums.ColorEntity;
import io.beanmapper.testmodel.enums.ColorResult;
import io.beanmapper.testmodel.enums.ColorStringResult;
import io.beanmapper.testmodel.enums.EnumSourceArraysAsList;
import io.beanmapper.testmodel.enums.EnumTargetList;
import io.beanmapper.testmodel.enums.RGB;
import io.beanmapper.testmodel.enums.UserRole;
import io.beanmapper.testmodel.enums.UserRoleResult;
import io.beanmapper.testmodel.enums.*;
import io.beanmapper.testmodel.ignore.IgnoreSource;
import io.beanmapper.testmodel.ignore.IgnoreTarget;
import io.beanmapper.testmodel.initially_unmatched_source.SourceWithUnmatchedField;
Expand Down Expand Up @@ -1626,6 +1619,14 @@ public void deepMapEnumInHolder() {
assertEquals(CountryEnum.NL.getImage(), target.country.image);
}

@Test
public void mapEnumWithToString() {
SourceWithEnumWithToString source = new SourceWithEnumWithToString();
source.myEnum = EnumWithToString.SOME_VALUE;
TargetWithEnumWithToString target = beanMapper.map(source, TargetWithEnumWithToString.class);
assertEquals(EnumWithToString.SOME_VALUE, target.myEnum);
}

public Person createPerson(String name) {
Person person = new Person();
person.setId(1984L);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.beanmapper.testmodel.enums;

public enum EnumWithToString {
SOME_VALUE;

public String toString() {
return "X" + name() + "X";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.beanmapper.testmodel.enums;

public class SourceWithEnumWithToString {
public EnumWithToString myEnum;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.beanmapper.testmodel.enums;

public class TargetWithEnumWithToString {
public EnumWithToString myEnum;
}

0 comments on commit 65a9628

Please sign in to comment.