From 0f5d790dbdc7c3855c742d6f94867c376352198d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 26 Jun 2014 11:24:55 +0200 Subject: [PATCH] DATAREST-331 - Fixed NullPointerException in ResourcesProcessorWrapper. During type matching in ResourcesProcessorWrapper we now accomodate the scenario that a Resources type is completely different than the Resources type to look for. This resulted in null being returned for the supertype generics lookup and this failed as the corresponding guard was missing. --- ...ceProcessorHandlerMethodReturnValueHandler.java | 8 +++++++- ...orHandlerMethodReturnValueHandlerUnitTests.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandler.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandler.java index 50a6ec29a..210d4912e 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandler.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandler.java @@ -439,7 +439,13 @@ public class ResourceProcessorHandlerMethodReturnValueHandler implements Handler Class resourcesType = resources.getClass(); - TypeInformation resourceTypeInformation = target.getSuperTypeInformation(resourcesType).getComponentType(); + TypeInformation superTypeInformation = target.getSuperTypeInformation(resourcesType); + + if (superTypeInformation == null) { + return false; + } + + TypeInformation resourceTypeInformation = superTypeInformation.getComponentType(); return ResourceProcessorWrapper.isValueTypeMatch((Resource) element, resourceTypeInformation); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandlerUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandlerUnitTests.java index 8a8a4a21e..d60602d17 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandlerUnitTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/ResourceProcessorHandlerMethodReturnValueHandlerUnitTests.java @@ -241,6 +241,20 @@ public class ResourceProcessorHandlerMethodReturnValueHandlerUnitTests { assertThat(projectionProcessor.invoked, is(true)); } + /** + * @see DATAREST-331 + */ + @Test + public void doesNotMatchOnNonMatchingResourcesTypes() throws Exception { + + Resource resource = new Resource(new Object()); + PagedResources> pagedResources = new PagedResources>( + Collections.singleton(resource), new PageMetadata(1, 0, 10)); + + TypeInformation type = ClassTypeInformation.from(RepositoryLinksResource.class); + assertThat(ResourcesProcessorWrapper.isValueTypeMatch(pagedResources, type), is(false)); + } + // Helpers ---------------------------------------------------------// private void invokeReturnValueHandler(String method, final Matcher matcher, Object returnValue) throws Exception { final MethodParameter methodParam = METHOD_PARAMS.get(method);