Skip to content

Commit

Permalink
Enforce match for resolved part of unresolvable target type
Browse files Browse the repository at this point in the history
Closes gh-34298
  • Loading branch information
jhoeller committed Jan 21, 2025
1 parent e9dc6be commit 90423a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -341,8 +341,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
}
// Full check for complex generic type match required?
ResolvableType rt = targetType.getResolvableType();
if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
!this.targetType.hasUnresolvableGenerics()) {
if (!(rt.getType() instanceof Class) && !rt.isAssignableFromResolvedPart(this.targetType)) {
return false;
}
return !(this.converter instanceof ConditionalConverter conditionalConverter) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -565,6 +565,22 @@ void rawCollectionAsSource() throws Exception {
assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")))).isEqualTo(Collections.singleton("testX"));
}

@Test
void stringListToListOfSubclassOfUnboundGenericClass() {
conversionService.addConverter(new StringListToAListConverter());
conversionService.addConverter(new StringListToBListConverter());

List<ARaw> aList = (List<ARaw>) conversionService.convert(List.of("foo"),
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)),
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(ARaw.class)));
assertThat(aList).allMatch(e -> e instanceof ARaw);

List<BRaw> bList = (List<BRaw>) conversionService.convert(List.of("foo"),
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)),
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(BRaw.class)));
assertThat(bList).allMatch(e -> e instanceof BRaw);
}


@ExampleAnnotation(active = true)
public String annotatedString;
Expand Down Expand Up @@ -742,6 +758,7 @@ public int getNestedMatchAttempts() {
}
}


private interface MyEnumBaseInterface {
String getBaseCode();
}
Expand Down Expand Up @@ -923,4 +940,33 @@ public Color convert(String source) {
return Color.decode(source.substring(0, 6));
}
}


private static class GenericBaseClass<T> {
}

private static class ARaw extends GenericBaseClass {
}

private static class BRaw extends GenericBaseClass {
}


private static class StringListToAListConverter implements Converter<List<String>, List<ARaw>> {

@Override
public List<ARaw> convert(List<String> source) {
return List.of(new ARaw());
}
}


private static class StringListToBListConverter implements Converter<List<String>, List<BRaw>> {

@Override
public List<BRaw> convert(List<String> source) {
return List.of(new BRaw());
}
}

}

0 comments on commit 90423a9

Please sign in to comment.