Skip to content

ObjectToObjectConverter doesn't consider return type of static methods #28609

@philwebb

Description

@philwebb

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();
		}

	}

}

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions