Ensure that ConversionService.canConvert(Enum) no longer throws an exception - #34532
Ensure that ConversionService.canConvert(Enum) no longer throws an exception#34532moonfruit wants to merge 1 commit into
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
sbrannen
left a comment
There was a problem hiding this comment.
Hi @moonfruit,
Congratulations on submitting your first PR for the Spring Framework! 👍
And thanks for bringing this to our attention.
I agree that canConvert() ideally should not throw an exception. To address that, I've requested some changes in the PR (see other comments). In addition, let's have IntegerToEnumConverterFactory and StringToEnumConverterFactory implement ConditionalConverter. Their implementations could then look something like this:
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return (ConversionUtils.resolveEnumType(targetType.getType()) != null);
}If you feel comfortable making these changes, go for it!
Otherwise, just let me know, and I can take it from here.
Cheers,
Sam
9034e73 to
910e4ed
Compare
|
Hi @sbrannen, I've updated the PR with all the requested changes:
|
910e4ed to
3ea78d4
Compare
Signed-off-by: MoonFruit <dkmoonfruit@gmail.com>
3ea78d4 to
643a9a0
Compare
|
Sorry about that — the commit was missing the |
Prior to this commit, ConversionService#canConvert(Class, Class) threw an IllegalArgumentException when invoked with Enum.class as the target type (i.e., `canConvert(String.class, Enum.class)`), because ConverterFactory#getConverter() in StringToEnumConverterFactory and IntegerToEnumConverterFactory eagerly resolved the concrete enum type. To address that, StringToEnumConverterFactory and IntegerToEnumConverterFactory now implement ConditionalConverter so that matches() can reject non-concrete-enum targets before getConverter() is ever invoked. Closes gh-34532 Signed-off-by: MoonFruit <dkmoonfruit@gmail.com> Co-authored-by: Sam Brannen <104798+sbrannen@users.noreply.github.com>
|
Fixed via e2fae06 |
|
This has been merged into Thanks |
In my project, I use the following code to find an appropriate method to execute for a given class. It throws an
IllegalArgumentException: The target type java.lang.Enum does not refer to an enumwhen there is a more appropriate “method”:asString(CharSequence).After doing some research I realized that the root cause was that
StringToEnumConverterFactory.getConverter(Enum.class)threw an exception when it couldn't convert and resolve the concreteEnum. This directly causesConversionService.canConvert(String.class,Enum.class)to throw an exception. I don't think the methodcanConvert()should throw an exception, it returnstrueif it can convert andfalseif it can't. That's what this method is for, to avoid throwing an exception when calling theconvertmethod directly.So I've modified
StringToEnumConverterFactoryandIntegerToEnumConverterFactorya little and hopefully this PR will be accepted.