Skip to content

Commit a8db5e3

Browse files
committed
DATACMNS-829 - Fixed handling of Map null values in ProxyProjectionFactory.
We now eagerly return null values in attempts to project Map values.
1 parent f8ea9fb commit a8db5e3

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/java/org/springframework/data/projection/ProjectingMethodInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,7 +137,7 @@ private Map<Object, Object> projectMapValues(Map<?, ?> sources, TypeInformation<
137137
}
138138

139139
private Object getProjection(Object result, Class<?> returnType) {
140-
return ClassUtils.isAssignable(returnType, result.getClass()) ? result
140+
return result == null || ClassUtils.isAssignable(returnType, result.getClass()) ? result
141141
: factory.createProjection(returnType, result);
142142
}
143143

src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import java.beans.PropertyDescriptor;
2222
import java.lang.reflect.Proxy;
23+
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
@@ -159,7 +160,7 @@ public void returnsAllPropertiesAsInputProperties() {
159160
ProjectionInformation projectionInformation = factory.getProjectionInformation(CustomerExcerpt.class);
160161
List<PropertyDescriptor> result = projectionInformation.getInputProperties();
161162

162-
assertThat(result, hasSize(5));
163+
assertThat(result, hasSize(6));
163164
}
164165

165166
/**
@@ -247,13 +248,30 @@ public void exposesProjectionInformationCorrectly() {
247248
assertThat(information.isClosed(), is(true));
248249
}
249250

251+
/**
252+
* @see DATACMNS-829
253+
*/
254+
@Test
255+
public void projectsMapOfStringToObjectCorrectly() {
256+
257+
Customer customer = new Customer();
258+
customer.data = Collections.singletonMap("key", null);
259+
260+
Map<String, Object> data = factory.createProjection(CustomerExcerpt.class, customer).getData();
261+
262+
assertThat(data, is(notNullValue()));
263+
assertThat(data.containsKey("key"), is(true));
264+
assertThat(data.get("key"), is(nullValue()));
265+
}
266+
250267
static class Customer {
251268

252269
public Long id;
253270
public String firstname, lastname;
254271
public Address address;
255272
public byte[] picture;
256273
public Address[] shippingAddresses;
274+
public Map<String, Object> data;
257275
}
258276

259277
static class Address {
@@ -272,6 +290,8 @@ interface CustomerExcerpt {
272290
AddressExcerpt[] getShippingAddresses();
273291

274292
byte[] getPicture();
293+
294+
Map<String, Object> getData();
275295
}
276296

277297
interface AddressExcerpt {

0 commit comments

Comments
 (0)