Skip to content

Commit 3302bc4

Browse files
committed
Merge branch '6.2.x'
2 parents 4763877 + 90423a9 commit 3302bc4

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.
@@ -334,8 +334,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
334334
}
335335
// Full check for complex generic type match required?
336336
ResolvableType rt = targetType.getResolvableType();
337-
if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
338-
!this.targetType.hasUnresolvableGenerics()) {
337+
if (!(rt.getType() instanceof Class) && !rt.isAssignableFromResolvedPart(this.targetType)) {
339338
return false;
340339
}
341340
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;
@@ -740,6 +756,7 @@ public int getNestedMatchAttempts() {
740756
}
741757
}
742758

759+
743760
private interface MyEnumBaseInterface {
744761
String getBaseCode();
745762
}
@@ -921,4 +938,33 @@ public Color convert(String source) {
921938
return Color.decode(source.substring(0, 6));
922939
}
923940
}
941+
942+
943+
private static class GenericBaseClass<T> {
944+
}
945+
946+
private static class ARaw extends GenericBaseClass {
947+
}
948+
949+
private static class BRaw extends GenericBaseClass {
950+
}
951+
952+
953+
private static class StringListToAListConverter implements Converter<List<String>, List<ARaw>> {
954+
955+
@Override
956+
public List<ARaw> convert(List<String> source) {
957+
return List.of(new ARaw());
958+
}
959+
}
960+
961+
962+
private static class StringListToBListConverter implements Converter<List<String>, List<BRaw>> {
963+
964+
@Override
965+
public List<BRaw> convert(List<String> source) {
966+
return List.of(new BRaw());
967+
}
968+
}
969+
924970
}

0 commit comments

Comments
 (0)