Skip to content

Fix GenericConversionService selects incorrect converter #34697

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 @@ -342,10 +342,50 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
}

public boolean matchesFallback(TypeDescriptor sourceType, TypeDescriptor targetType) {
return (this.typeInfo.getTargetType() == targetType.getObjectType() &&
this.targetType.hasUnresolvableGenerics() &&
(!(this.converter instanceof ConditionalConverter conditionalConverter) ||
conditionalConverter.matches(sourceType, targetType)));
if (this.typeInfo.getTargetType() != targetType.getObjectType()) {
return false;
}

if (!this.targetType.hasUnresolvableGenerics()) {
return false;
}

boolean assignable = isAssignableTo(targetType.getResolvableType());
if (!assignable) {
return false;
}

return !(this.converter instanceof ConditionalConverter conditionalConverter) ||
conditionalConverter.matches(sourceType, targetType);
}

private boolean isAssignableTo(ResolvableType expected) {
return isAssignable(this.targetType, expected);
}
Comment on lines +362 to +364
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If use TypeDescriptor.isAssignableTo

private boolean isAssignableTo(TypeDescriptor targetType) {
	TypeDescriptor converterTargetType = new TypeDescriptor(this.targetType, this.targetType.toClass(), null);
	return converterTargetType.isAssignableTo(targetType);
}


private boolean isAssignable(ResolvableType actual, ResolvableType expected) {
Class<?> actualResolved = actual.resolve(Object.class);
Class<?> expectedResolved = expected.resolve(Object.class);
if (!expectedResolved.isAssignableFrom(actualResolved)) {
return false;
}

ResolvableType[] actualGenerics = actual.getGenerics();
ResolvableType[] expectedGenerics = expected.getGenerics();

if (actualGenerics.length != expectedGenerics.length) {
return false;
}

for (int i = 0; i < actualGenerics.length; i++) {
ResolvableType actualGeneric = actualGenerics[i];
ResolvableType expectedGeneric = expectedGenerics[i];
if (!isAssignable(actualGeneric, expectedGeneric)) {
return false;
}
}

return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,19 @@ void stringToListOfMapConverterWithFallbackMatch() {
assertThat("foo").isEqualTo(result.get(0).get("bar"));
}

@Test
@SuppressWarnings("unchecked")
void stringToListOfString() {
conversionService.addConverter(new StringToCollectionConverter(conversionService));
conversionService.addConverter(new StringToListOfMapConverter());

List<String> result = (List<String>) conversionService.convert("foo,bar",
TypeDescriptor.valueOf(String.class),
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class))
);

assertThat(result.get(0)).isEqualTo("foo");
}

@ExampleAnnotation(active = true)
public String annotatedString;
Expand Down Expand Up @@ -990,5 +1003,4 @@ private static class StringToListOfMapConverter implements Converter<String, Lis
return List.of(Map.of("bar", source));
}
}

}