Skip to content

Commit db623a8

Browse files
authored
Fix #4443: detect Iterable as IterationType (#4487)
1 parent a479197 commit db623a8

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Project: jackson-databind
66

77
2.18.0 (not yet released)
88

9+
#4443: Add `Iterable<T>` as recognized `IterationType` instance (along with
10+
`Iterable`, `Stream`)
911
#4453: Allow JSON Integer to deserialize into a single-arg constructor of
1012
parameter type `double`
1113
(contributed by David M)

src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,9 @@ protected JavaType _fromWellKnownClass(ClassStack context, Class<?> rawType, Typ
16371637
// detected, related to difficulties in propagating type upwards (Iterable, for
16381638
// example, is a weak, tag-on type). They may be detectable in future.
16391639
// 23-May-2023, tatu: As of 2.16 we do, however, recognized certain `IterationType`s.
1640-
if (rawType == Iterator.class || rawType == Stream.class) {
1640+
if (rawType == Iterator.class || rawType == Stream.class
1641+
// 18-Apr-2024, tatu: [databind#4443] allow exact `Iterable`
1642+
|| rawType == Iterable.class) {
16411643
return _iterationType(rawType, bindings, superClass, superInterfaces);
16421644
}
16431645
if (BaseStream.class.isAssignableFrom(rawType)) {

src/test/java/com/fasterxml/jackson/databind/type/JavaTypeTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ public void testIterationTypesDirect() throws Exception
324324
Iterator.class, Object.class);
325325
_verifyIteratorType(tf.constructType(Stream.class),
326326
Stream.class, Object.class);
327+
_verifyIteratorType(tf.constructType(Iterable.class),
328+
Iterable.class, Object.class);
327329

328330
// Then generic but direct
329331
JavaType t = _verifyIteratorType(tf.constructType(new TypeReference<Iterator<String>>() { }),

src/test/java/com/fasterxml/jackson/databind/type/TypeFactoryTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public void testIterator()
165165
assertNull(t.containedType(1));
166166
}
167167

168+
// [databind#4443]
169+
@Test
170+
public void testIterable()
171+
{
172+
TypeFactory tf = TypeFactory.defaultInstance();
173+
JavaType t = tf.constructType(new TypeReference<Iterable<String>>() { });
174+
assertEquals(IterationType.class, t.getClass());
175+
assertTrue(t.isIterationType());
176+
assertSame(Iterable.class, t.getRawClass());
177+
assertEquals(1, t.containedTypeCount());
178+
assertEquals(tf.constructType(String.class), t.containedType(0));
179+
assertNull(t.containedType(1));
180+
}
168181
/**
169182
* Test for verifying that parametric types can be constructed
170183
* programmatically

0 commit comments

Comments
 (0)