-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
ObjectToObjectConverter doesn't consider return type of static methods #28609
Comments
The following test cases pass with the change (to verify compatible return type for static factory methods). class ObjectToObjectConverterTests {
private final GenericConversionService conversionService = new GenericConversionService() {{
addConverter(new ObjectToObjectConverter());
}};
/**
* This test effectively verifies that the {@link ObjectToObjectConverter}
* was properly registered with the {@link GenericConversionService}.
*/
@Test
void nonStaticToTargetTypeSimpleNameMethodWithMatchingReturnType() {
assertThat(conversionService.canConvert(Source.class, Data.class))
.as("can convert Source to Data").isTrue();
Data data = conversionService.convert(new Source("test"), Data.class);
assertThat(data).asString().isEqualTo("test");
}
@Test
void nonStaticToTargetTypeSimpleNameMethodWithDifferentReturnType() {
assertThat(conversionService.canConvert(Text.class, Data.class))
.as("can convert Text to Data").isFalse();
assertThat(conversionService.canConvert(Text.class, Optional.class))
.as("can convert Text to Optional").isFalse();
assertThatExceptionOfType(ConverterNotFoundException.class)
.as("convert Text to Data")
.isThrownBy(() -> conversionService.convert(new Text("test"), Data.class));
}
@Test
void staticValueOfFactoryMethodWithDifferentReturnType() {
assertThat(conversionService.canConvert(String.class, Data.class))
.as("can convert String to Data").isFalse();
assertThatExceptionOfType(ConverterNotFoundException.class)
.as("convert String to Data")
.isThrownBy(() -> conversionService.convert("test", Data.class));
}
static class Source {
private final String value;
private Source(String value) {
this.value = value;
}
public Data toData() {
return new Data(this.value);
}
}
static class Text {
private final String value;
private Text(String value) {
this.value = value;
}
public Optional<Data> toData() {
return Optional.of(new Data(this.value));
}
}
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();
}
}
} But... I believe the new behavior is desired, but this will require some clarification since it may be a breaking change for applications that depend on the current behavior. |
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 forcanConvert
to returntrue
when the actual conversion would throw an exception.The following test shows the problem:
The text was updated successfully, but these errors were encountered: