Skip to content

Commit e496c19

Browse files
mp911dechristophstrobl
authored andcommitted
Consider target type when calling getAllOf(…).
We now accept a type hint when calling getAllOf(…) to avoid materializing null instances when the actual type hint cannot resolve to an entity. Closes #1995 Original Pull Request: #1996
1 parent 754fac9 commit e496c19

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,21 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
373373
*/
374374
@Override
375375
public List<?> getAllOf(String keyspace) {
376-
return getAllOf(keyspace, -1, -1);
376+
return getAllOf(keyspace, Object.class, -1, -1);
377377
}
378378

379-
public List<?> getAllOf(String keyspace, long offset, int rows) {
379+
@Override
380+
public <T> Iterable<T> getAllOf(String keyspace, Class<T> type) {
381+
return getAllOf(keyspace, type, -1, -1);
382+
}
383+
384+
public <T> List<T> getAllOf(String keyspace, Class<T> type, long offset, int rows) {
380385

381386
byte[] binKeyspace = toBytes(keyspace);
382387

383388
Set<byte[]> ids = redisOps.execute((RedisCallback<Set<byte[]>>) connection -> connection.sMembers(binKeyspace));
384389

385-
List<Object> result = new ArrayList<>();
390+
List<T> result = new ArrayList<>();
386391
List<byte[]> keys = new ArrayList<>(ids);
387392

388393
if (keys.isEmpty() || keys.size() < offset) {
@@ -395,7 +400,7 @@ public List<?> getAllOf(String keyspace, long offset, int rows) {
395400
}
396401

397402
for (byte[] key : keys) {
398-
result.add(get(key, keyspace));
403+
result.add(get(key, keyspace, type));
399404
}
400405
return result;
401406
}

src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public <T> Collection<T> execute(RedisOperationChain criteria, Comparator<?> sor
8080
if (criteria == null
8181
|| (CollectionUtils.isEmpty(criteria.getOrSismember()) && CollectionUtils.isEmpty(criteria.getSismember()))
8282
&& criteria.getNear() == null) {
83-
return (Collection<T>) getAdapter().getAllOf(keyspace, offset, rows);
83+
return getAdapter().getAllOf(keyspace, type, offset, rows);
8484
}
8585

8686
RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = connection -> {

src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import org.springframework.data.annotation.Id;
3636
import org.springframework.data.annotation.Reference;
37+
import org.springframework.data.annotation.TypeAlias;
3738
import org.springframework.data.geo.Point;
3839
import org.springframework.data.keyvalue.annotation.KeySpace;
3940
import org.springframework.data.redis.connection.RedisConnection;
@@ -62,6 +63,7 @@ public class RedisKeyValueAdapterTests {
6263
private RedisKeyValueAdapter adapter;
6364
private StringRedisTemplate template;
6465
private RedisConnectionFactory connectionFactory;
66+
private RedisMappingContext mappingContext;
6567

6668
public RedisKeyValueAdapterTests(RedisConnectionFactory connectionFactory) throws Exception {
6769
this.connectionFactory = connectionFactory;
@@ -73,7 +75,7 @@ void setUp() {
7375
template = new StringRedisTemplate(connectionFactory);
7476
template.afterPropertiesSet();
7577

76-
RedisMappingContext mappingContext = new RedisMappingContext(
78+
mappingContext = new RedisMappingContext(
7779
new MappingConfiguration(new IndexConfiguration(), new KeyspaceConfiguration()));
7880
mappingContext.afterPropertiesSet();
7981

@@ -225,6 +227,34 @@ void getShouldReadNestedObjectCorrectly() {
225227
assertThat(((Person) loaded).address.country).isEqualTo("Andor");
226228
}
227229

230+
@Test // #1995
231+
void getAllOfShouldReturnSuperTypeIfForUnregisteredTypeAlias() {
232+
233+
Map<String, String> map = new LinkedHashMap<>();
234+
map.put("_class", "taveren");
235+
map.put("address.country", "Andor");
236+
template.opsForHash().putAll("persons:load-1", map);
237+
238+
Object loaded = adapter.get("load-1", "persons", Person.class);
239+
240+
assertThat(loaded).isExactlyInstanceOf(Person.class);
241+
}
242+
243+
@Test // #1995
244+
void getAllOfShouldReturnCorrectTypeIfForRegisteredTypeAlias() {
245+
246+
mappingContext.getPersistentEntity(TaVeren.class);
247+
248+
Map<String, String> map = new LinkedHashMap<>();
249+
map.put("_class", "taveren");
250+
map.put("address.country", "Andor");
251+
template.opsForHash().putAll("persons:load-1", map);
252+
253+
Object loaded = adapter.get("load-1", "persons", Person.class);
254+
255+
assertThat(loaded).isExactlyInstanceOf(TaVeren.class);
256+
}
257+
228258
@Test // DATAREDIS-425
229259
void couldReadsKeyspaceSizeCorrectly() {
230260

@@ -826,6 +856,7 @@ static class AddressWithPostcode extends Address {
826856
String postcode;
827857
}
828858

859+
@TypeAlias("taveren")
829860
static class TaVeren extends Person {
830861

831862
}

0 commit comments

Comments
 (0)