Skip to content

Commit 0f85f1c

Browse files
committed
Prefer explicitly declared generic parameter over Iterable in TypeDiscoverer.getComponentType().
Related ticket: #2312.
1 parent 07342cf commit 0f85f1c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Diff for: src/main/java/org/springframework/data/util/TypeDiscoverer.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ protected TypeInformation<?> doGetComponentType() {
148148
return getTypeArgument(CustomCollections.getMapBaseType(rawType), 0);
149149
}
150150

151+
List<TypeInformation<?>> arguments = getTypeArguments();
152+
153+
if (arguments.size() > 0) {
154+
return arguments.get(0);
155+
}
156+
151157
if (Iterable.class.isAssignableFrom(rawType)) {
152158
return getTypeArgument(Iterable.class, 0);
153159
}
@@ -156,9 +162,7 @@ protected TypeInformation<?> doGetComponentType() {
156162
return getTypeArgument(rawType, 0);
157163
}
158164

159-
List<TypeInformation<?>> arguments = getTypeArguments();
160-
161-
return arguments.size() > 0 ? arguments.get(0) : null;
165+
return null;
162166
}
163167

164168
@Override

Diff for: src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.mockito.junit.jupiter.MockitoExtension;
3232
import org.springframework.beans.factory.annotation.Autowire;
3333
import org.springframework.core.ResolvableType;
34+
import org.springframework.data.geo.GeoResults;
3435
import org.springframework.util.ReflectionUtils;
3536

3637
/**
@@ -326,6 +327,14 @@ void detectsMapKeyAndValueOnEnumMaps() {
326327
assertThat(map.getMapValueType().getType()).isEqualTo(String.class);
327328
}
328329

330+
@Test
331+
void detectsComponentTypeOfTypedClass() {
332+
333+
TypeInformation<?> property = TypeInformation.of(GeoResultsWrapper.class).getProperty("results");
334+
335+
assertThat(property.getComponentType().getType()).isEqualTo(Leaf.class);
336+
}
337+
329338
class Person {
330339

331340
Addresses addresses;
@@ -415,4 +424,8 @@ class Leaf {}
415424
class EnumMapWrapper {
416425
EnumMap<Autowire, String> map;
417426
}
427+
428+
class GeoResultsWrapper {
429+
GeoResults<Leaf> results;
430+
}
418431
}

0 commit comments

Comments
 (0)