From 79048e18be0570c2744bed2a0c31626b7cb267f0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 18 Oct 2013 10:56:04 -0700 Subject: [PATCH] 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 --- .../core/convert/TypeDescriptor.java | 10 ++++++---- .../core/convert/TypeDescriptorTests.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java index 96d6c97538..038a91c913 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java +++ b/spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java @@ -498,12 +498,13 @@ public class TypeDescriptor implements Serializable { */ public static TypeDescriptor collection(Class collectionType, TypeDescriptor elementTypeDescriptor) { Assert.notNull(collectionType, "CollectionType must not be null"); - Assert.notNull(elementTypeDescriptor, "ElementTypeDesciptor must not be null"); if (!Collection.class.isAssignableFrom(collectionType)) { throw new IllegalArgumentException("collectionType must be a java.util.Collection"); } + ResolvableType element = (elementTypeDescriptor == null ? null + : elementTypeDescriptor.resolvableType); return new TypeDescriptor(ResolvableType.forClassWithGenerics(collectionType, - elementTypeDescriptor.resolvableType), null, null); + element), null, null); } /** @@ -520,8 +521,9 @@ public class TypeDescriptor implements Serializable { if (!Map.class.isAssignableFrom(mapType)) { throw new IllegalArgumentException("mapType must be a java.util.Map"); } - return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, - keyTypeDescriptor.resolvableType, valueTypeDescriptor.resolvableType), null, null); + ResolvableType key = (keyTypeDescriptor == null ? null : keyTypeDescriptor.resolvableType); + ResolvableType value = (valueTypeDescriptor == null ? null : valueTypeDescriptor.resolvableType); + return new TypeDescriptor(ResolvableType.forClassWithGenerics(mapType, key, value), null, null); } /** diff --git a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java index 22107b085d..c7de261533 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java @@ -918,4 +918,18 @@ public class TypeDescriptorTests { TypeDescriptor readObject = (TypeDescriptor) inputStream.readObject(); assertThat(readObject, equalTo(typeDescriptor)); } + + @Test + public void createCollectionWithNullElement() throws Exception { + TypeDescriptor typeDescriptor = TypeDescriptor.collection(List.class, null); + assertThat(typeDescriptor.getElementTypeDescriptor(), nullValue()); + } + + @Test + public void createMapWithNullElements() throws Exception { + TypeDescriptor typeDescriptor = TypeDescriptor.map(LinkedHashMap.class, null, null); + assertThat(typeDescriptor.getMapKeyTypeDescriptor(), nullValue()); + assertThat(typeDescriptor.getMapValueTypeDescriptor(), nullValue()); + } + }