Skip to content

Commit 90423a9

Browse files
committedJan 21, 2025
Enforce match for resolved part of unresolvable target type
Closes gh-34298
1 parent e9dc6be commit 90423a9

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed
 

‎spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -341,8 +341,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
341341
}
342342
// Full check for complex generic type match required?
343343
ResolvableType rt = targetType.getResolvableType();
344-
if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
345-
!this.targetType.hasUnresolvableGenerics()) {
344+
if (!(rt.getType() instanceof Class) && !rt.isAssignableFromResolvedPart(this.targetType)) {
346345
return false;
347346
}
348347
return !(this.converter instanceof ConditionalConverter conditionalConverter) ||

‎spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -565,6 +565,22 @@ void rawCollectionAsSource() throws Exception {
565565
assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")))).isEqualTo(Collections.singleton("testX"));
566566
}
567567

568+
@Test
569+
void stringListToListOfSubclassOfUnboundGenericClass() {
570+
conversionService.addConverter(new StringListToAListConverter());
571+
conversionService.addConverter(new StringListToBListConverter());
572+
573+
List<ARaw> aList = (List<ARaw>) conversionService.convert(List.of("foo"),
574+
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)),
575+
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(ARaw.class)));
576+
assertThat(aList).allMatch(e -> e instanceof ARaw);
577+
578+
List<BRaw> bList = (List<BRaw>) conversionService.convert(List.of("foo"),
579+
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)),
580+
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(BRaw.class)));
581+
assertThat(bList).allMatch(e -> e instanceof BRaw);
582+
}
583+
568584

569585
@ExampleAnnotation(active = true)
570586
public String annotatedString;
@@ -742,6 +758,7 @@ public int getNestedMatchAttempts() {
742758
}
743759
}
744760

761+
745762
private interface MyEnumBaseInterface {
746763
String getBaseCode();
747764
}
@@ -923,4 +940,33 @@ public Color convert(String source) {
923940
return Color.decode(source.substring(0, 6));
924941
}
925942
}
943+
944+
945+
private static class GenericBaseClass<T> {
946+
}
947+
948+
private static class ARaw extends GenericBaseClass {
949+
}
950+
951+
private static class BRaw extends GenericBaseClass {
952+
}
953+
954+
955+
private static class StringListToAListConverter implements Converter<List<String>, List<ARaw>> {
956+
957+
@Override
958+
public List<ARaw> convert(List<String> source) {
959+
return List.of(new ARaw());
960+
}
961+
}
962+
963+
964+
private static class StringListToBListConverter implements Converter<List<String>, List<BRaw>> {
965+
966+
@Override
967+
public List<BRaw> convert(List<String> source) {
968+
return List.of(new BRaw());
969+
}
970+
}
971+
926972
}

0 commit comments

Comments
 (0)