diff --git a/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java b/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java index 013522815..f09c347a4 100644 --- a/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java +++ b/src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -137,7 +137,7 @@ class ProjectingMethodInterceptor implements MethodInterceptor { } private Object getProjection(Object result, Class returnType) { - return ClassUtils.isAssignable(returnType, result.getClass()) ? result + return result == null || ClassUtils.isAssignable(returnType, result.getClass()) ? result : factory.createProjection(returnType, result); } diff --git a/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java b/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java index fea16e62c..67ce6f000 100644 --- a/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java +++ b/src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.lang.reflect.Proxy; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -157,7 +158,7 @@ public class ProxyProjectionFactoryUnitTests { List result = factory.getInputProperties(CustomerExcerpt.class); - assertThat(result, hasSize(5)); + assertThat(result, hasSize(6)); assertThat(result, hasItems("firstname", "address", "shippingAddresses", "picture")); } @@ -222,6 +223,34 @@ public class ProxyProjectionFactoryUnitTests { assertThat(excerpt.getId(), is(customer.id.toString())); } + /** + * @see DATACMNS-89 + */ + @Test + public void exposesProjectionInformationCorrectly() { + + ProjectionInformation information = factory.getProjectionInformation(CustomerExcerpt.class); + + assertThat(information.getType(), is(typeCompatibleWith(CustomerExcerpt.class))); + assertThat(information.isClosed(), is(true)); + } + + /** + * @see DATACMNS-829 + */ + @Test + public void projectsMapOfStringToObjectCorrectly() { + + Customer customer = new Customer(); + customer.data = Collections.singletonMap("key", null); + + Map data = factory.createProjection(CustomerExcerpt.class, customer).getData(); + + assertThat(data, is(notNullValue())); + assertThat(data.containsKey("key"), is(true)); + assertThat(data.get("key"), is(nullValue())); + } + static class Customer { public Long id; @@ -229,6 +258,7 @@ public class ProxyProjectionFactoryUnitTests { public Address address; public byte[] picture; public Address[] shippingAddresses; + public Map data; } static class Address { @@ -247,6 +277,8 @@ public class ProxyProjectionFactoryUnitTests { AddressExcerpt[] getShippingAddresses(); byte[] getPicture(); + + Map getData(); } interface AddressExcerpt {