Skip to content

SPR-9692: Change to match converter to interface before Enum #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ else if (componentType.isInterface()) {
return converter;
}
Class<?> superClass = currentClass.getSuperclass();
if (superClass != null && superClass != Object.class) {
if (superClass != null && superClass != Object.class && superClass != Enum.class) {
classQueue.addFirst(superClass);
}
for (Class<?> interfaceType : currentClass.getInterfaces()) {
Expand All @@ -365,6 +365,14 @@ else if (componentType.isInterface()) {
return converter;
}
}
if (sourceObjectType.isEnum()) {
Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(Enum.class);
GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters);
if (converter != null) {
return converter;
}
}

Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class);
return getMatchingConverterForTarget(sourceType, targetType, objectConverters);
}
Expand Down Expand Up @@ -430,7 +438,7 @@ else if (componentType.isInterface()) {
return converter;
}
Class<?> superClass = currentClass.getSuperclass();
if (superClass != null && superClass != Object.class) {
if (superClass != null && superClass != Object.class && superClass != Enum.class) {
classQueue.addFirst(superClass);
}
for (Class<?> interfaceType : currentClass.getInterfaces()) {
Expand All @@ -444,6 +452,12 @@ else if (componentType.isInterface()) {
return converter;
}
}
if (targetObjectType.isEnum()) {
GenericConverter converter = matchConverter(converters.get(Enum.class), sourceType, targetType);
if (converter != null) {
return converter;
}
}
return matchConverter(converters.get(Object.class), sourceType, targetType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -589,5 +590,80 @@ public void testConvertiblePairDifferentEqualsAndHash() throws Exception {
assertFalse(pair.equals(pairOpposite));
assertFalse(pair.hashCode() == pairOpposite.hashCode());
}

// SPR-9692
@Test
public void testStringToEnumWithInterfaceConversion() {
conversionService.addConverterFactory(new StringToEnumConverterFactory());
conversionService.addConverterFactory(new StringToMyEnumInterfaceConverterFactory());
MyEnum result = conversionService.convert("1", MyEnum.class);
assertEquals(MyEnum.A, result);
}

@Test
public void testEnumWithInterfaceToStringConversion() {
conversionService.addConverter(new EnumToStringConverter());
conversionService.addConverter(new MyEnumInterfaceToStringConverter<MyEnum>());
String result = conversionService.convert(MyEnum.A, String.class);
assertEquals("1", result);
}

interface MyEnumInterface {
String getCode();
}

public static enum MyEnum implements MyEnumInterface {
A("1"), B("2");

private String code;

MyEnum(String code) {
this.code = code;
}

public String getCode() {
return code;
}
}

public static class MyEnumInterfaceToStringConverter<T extends MyEnumInterface> implements Converter<T, String> {
public String convert(T source) {
return source.getCode();
}
}

public static class StringToMyEnumInterfaceConverterFactory implements ConverterFactory<String, MyEnumInterface> {

public <T extends MyEnumInterface> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToMyEnumInterfaceConverter(targetType);
}

private static class StringToMyEnumInterfaceConverter<T extends Enum & MyEnumInterface> implements Converter<String, T> {
private final Class<T> enumType;

public StringToMyEnumInterfaceConverter(Class<T> enumType) {
this.enumType = enumType;
}

public T convert(String source) {
for (T value : enumType.getEnumConstants()) {
if (value.getCode().equals(source)) {
return value;
}
}
return null;
}
}
}

private static class StringToMyEnumInterfaceConverter implements Converter<String, MyEnumInterface> {
public MyEnumInterface convert(String source) {
for (MyEnumInterface value : MyEnum.values()) {
if (value.getCode().equals(source)) {
return value;
}
}
return null;
}
}
}