From 109e7f903e5970a6eb6c531a04a56e25cb130d3f Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 11 Apr 2012 07:58:03 +0200 Subject: [PATCH] DATACMNS-132 - Guard against untyped Maps and Collections in AbstractPersistentProperty.isEntity(). Just mentioned method threw a NullPointerException when the property was backed by an untyped Map or Collection as TypeInformation.getActualType() returns null in this case. Changed that to correctly return false. --- .../model/AbstractPersistentProperty.java | 3 ++- .../AbstractPersistentPropertyUnitTests.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java index efe954d00..da3d91c6c 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java @@ -177,7 +177,8 @@ public abstract class AbstractPersistentProperty

protected boolean isEntity() { - boolean isComplexType = !simpleTypeHolder.isSimpleType(information.getActualType().getType()); + TypeInformation actualType = information.getActualType(); + boolean isComplexType = actualType == null ? false : !simpleTypeHolder.isSimpleType(actualType.getType()); return isComplexType && !isTransient() && !isCollectionLike() && !isMap(); } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java index 393a18265..0fc1956f7 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/model/AbstractPersistentPropertyUnitTests.java @@ -5,6 +5,8 @@ import static org.junit.Assert.*; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Map; import java.util.TreeSet; import org.junit.Before; @@ -54,14 +56,39 @@ public class AbstractPersistentPropertyUnitTests { assertThat(property.getPersistentEntityType().iterator().hasNext(), is(false)); } + /** + * @see DATACMNS-132 + */ + @Test + public void isEntityWorksForUntypedMaps() throws Exception { + + Field field = ReflectionUtils.findField(TestClassComplex.class, "map"); + SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder); + assertThat(property.isEntity(), is(false)); + } + + /** + * @see DATACMNS-132 + */ + @Test + public void isEntityWorksForUntypedCollection() throws Exception { + + Field field = ReflectionUtils.findField(TestClassComplex.class, "collection"); + SamplePersistentProperty property = new SamplePersistentProperty(field, null, entity, typeHolder); + assertThat(property.isEntity(), is(false)); + } + @SuppressWarnings("serial") class TestClassSet extends TreeSet { } + @SuppressWarnings("rawtypes") class TestClassComplex { String id; TestClassSet testClassSet; + Map map; + Collection collection; } class SamplePersistentProperty extends AbstractPersistentProperty {