Skip to content

Ensure that ConversionService.canConvert(Enum) no longer throws an exception #34532

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -21,8 +21,9 @@
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
Expand Down Expand Up @@ -71,13 +72,30 @@ public static boolean canConvertElements(@Nullable TypeDescriptor sourceElementT
return false;
}

public static Class<?> getEnumType(Class<?> targetType) {
public static @Nullable Class<?> getEnumType(Class<?> targetType) {
Class<?> enumType = targetType;
while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass();
}
Assert.notNull(enumType, () -> "The target type " + targetType.getName() + " does not refer to an enum");
return enumType;
}

static class NonConvertableToEnum<T> implements Converter<String, T>, ConditionalConverter {

private final Class<T> targetType;

public NonConvertableToEnum(Class<T> targetType) {
this.targetType = targetType;
}

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return false;
}

@Override
public @Nullable T convert(String source) {
throw new IllegalArgumentException("The target type " + targetType.getName() + " does not refer to an enum");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ final class IntegerToEnumConverterFactory implements ConverterFactory<Integer, E

@Override
public <T extends Enum> Converter<Integer, T> getConverter(Class<T> targetType) {
return new IntegerToEnum(ConversionUtils.getEnumType(targetType));
Class<?> enumType = ConversionUtils.getEnumType(targetType);
if (enumType == null) {
return new ConversionUtils.NonConvertableToEnum(targetType);
}
return new IntegerToEnum(enumType);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ final class StringToEnumConverterFactory implements ConverterFactory<String, Enu

@Override
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToEnum(ConversionUtils.getEnumType(targetType));
Class<?> enumType = ConversionUtils.getEnumType(targetType);
if (enumType == null) {
return new ConversionUtils.NonConvertableToEnum(targetType);
}
return new StringToEnum(enumType);
}


private static class StringToEnum<T extends Enum> implements Converter<String, T> {

private final Class<T> enumType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,16 @@ void stringToEnumWithBaseInterfaceConversion() {
assertThat(conversionService.convert("base1", MyEnum.class)).isEqualTo(MyEnum.A);
}

@Test
void toEnumCanConvertShouldNotThrowException() {
conversionService.addConverterFactory(new IntegerToEnumConverterFactory());
conversionService.addConverterFactory(new StringToEnumConverterFactory());
assertThat(conversionService.canConvert(Integer.class, Enum.class)).isFalse();
assertThat(conversionService.canConvert(Integer.class, MyEnum.class)).isTrue();
assertThat(conversionService.canConvert(String.class, Enum.class)).isFalse();
assertThat(conversionService.canConvert(String.class, MyEnum.class)).isTrue();
}

@Test
void convertNullAnnotatedStringToString() throws Exception {
String source = null;
Expand Down