The ObjectToObject converter currently checks the return type for non-static methods; however, it does no such check for static methods. This means that it's possible for canConvert to return true when the actual conversion would throw an exception.
The following test shows the problem:
public class ObjectToObjectConverterTests {
@Test
void fromWithDifferentReturnType() {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new ObjectToObjectConverter());
// assertThat(conversionService.canConvert(String.class, Data.class)).isFalse();
Data data = conversionService.convert("test", Data.class);
}
static class Data {
private final String value;
private Data(String value) {
this.value = value;
}
@Override
public String toString() {
return this.value;
}
public static Optional<Data> valueOf(String string) {
return (string != null) ? Optional.of(new Data(string)) : Optional.empty();
}
}
}
The
ObjectToObjectconverter currently checks the return type for non-static methods; however, it does no such check for static methods. This means that it's possible forcanConvertto returntruewhen the actual conversion would throw an exception.The following test shows the problem: