Skip to content

Commit 79048e1

Browse files
author
Phillip Webb
committed
Allow TypeDescriptor create with null generics
Restore the ability to create a TypeDescriptor for a collection or map where the generics may be null. Issue: SPR-11006
1 parent cfb6625 commit 79048e1

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,13 @@ public static TypeDescriptor valueOf(Class<?> type) {
498498
*/
499499
public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementTypeDescriptor) {
500500
Assert.notNull(collectionType, "CollectionType must not be null");
501-
Assert.notNull(elementTypeDescriptor, "ElementTypeDesciptor must not be null");
502501
if (!Collection.class.isAssignableFrom(collectionType)) {
503502
throw new IllegalArgumentException("collectionType must be a java.util.Collection");
504503
}
504+
ResolvableType element = (elementTypeDescriptor == null ? null
505+
: elementTypeDescriptor.resolvableType);
505506
return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType,
506-
elementTypeDescriptor.resolvableType), null, null);
507+
element), null, null);
507508
}
508509

509510
/**
@@ -520,8 +521,9 @@ public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyTypeDescrip
520521
if (!Map.class.isAssignableFrom(mapType)) {
521522
throw new IllegalArgumentException("mapType must be a java.util.Map");
522523
}
523-
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType,
524-
keyTypeDescriptor.resolvableType, valueTypeDescriptor.resolvableType), null, null);
524+
ResolvableType key = (keyTypeDescriptor == null ? null : keyTypeDescriptor.resolvableType);
525+
ResolvableType value = (valueTypeDescriptor == null ? null : valueTypeDescriptor.resolvableType);
526+
return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null);
525527
}
526528

527529
/**

spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,18 @@ public void serializable() throws Exception {
918918
TypeDescriptor readObject = (TypeDescriptor) inputStream.readObject();
919919
assertThat(readObject, equalTo(typeDescriptor));
920920
}
921+
922+
@Test
923+
public void createCollectionWithNullElement() throws Exception {
924+
TypeDescriptor typeDescriptor = TypeDescriptor.collection(List.class, null);
925+
assertThat(typeDescriptor.getElementTypeDescriptor(), nullValue());
926+
}
927+
928+
@Test
929+
public void createMapWithNullElements() throws Exception {
930+
TypeDescriptor typeDescriptor = TypeDescriptor.map(LinkedHashMap.class, null, null);
931+
assertThat(typeDescriptor.getMapKeyTypeDescriptor(), nullValue());
932+
assertThat(typeDescriptor.getMapValueTypeDescriptor(), nullValue());
933+
}
934+
921935
}

0 commit comments

Comments
 (0)