From a2d11011daf05445e6c1d3b2d99617cbe02a908e Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 7 Mar 2017 14:25:47 +0100 Subject: [PATCH] DATAREST-1018 - Prevent NullPointerException in UriToEntityConverter. In case PersistentEntities exposes a managed type whose raw type currently doesn't have a PersistentEntity registered, the constructor of UriToEntityConverter ran into a NullPointerException. We now explicitly check for null and skip those types. Filed DATAREST-1021 for further improvements in the 3.0 time frame. --- .../data/rest/core/UriToEntityConverter.java | 2 +- .../rest/core/UriToEntityConverterUnitTests.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/UriToEntityConverter.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/UriToEntityConverter.java index 0c1bdb894..cdaeea02b 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/UriToEntityConverter.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/UriToEntityConverter.java @@ -68,7 +68,7 @@ public class UriToEntityConverter implements ConditionalGenericConverter { Class rawType = domainType.getType(); PersistentEntity entity = entities.getPersistentEntity(rawType); - if (entity.hasIdProperty()) { + if (entity != null && entity.hasIdProperty()) { convertiblePairs.add(new ConvertiblePair(URI.class, domainType.getType())); } } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/UriToEntityConverterUnitTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/UriToEntityConverterUnitTests.java index fec4f407c..2c1de48cc 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/UriToEntityConverterUnitTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/UriToEntityConverterUnitTests.java @@ -39,6 +39,7 @@ import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.support.Repositories; import org.springframework.data.repository.support.RepositoryInvoker; import org.springframework.data.repository.support.RepositoryInvokerFactory; +import org.springframework.data.util.ClassTypeInformation; /** * Unit tests for {@link UriToEntityConverter}. @@ -134,6 +135,19 @@ public class UriToEntityConverterUnitTests { new UriToEntityConverter(mock(PersistentEntities.class), invokerFactory, null); } + /** + * @see DATAREST-1018 + */ + @Test + @SuppressWarnings("unchecked") + public void doesNotRegisterTypeWithUnmanagedRawType() { + + PersistentEntities entities = mock(PersistentEntities.class); + doReturn(Arrays.asList(ClassTypeInformation.OBJECT)).when(entities).getManagedTypes(); + + new UriToEntityConverter(entities, invokerFactory, repositories); + } + static class Entity { @Id String id; }