Skip to content

Consider target type when calling getAllOf(…) #1996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.0-SNAPSHOT</version>
<version>2.5.0-GH-1995-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand All @@ -16,7 +18,7 @@
</parent>

<properties>
<springdata.keyvalue>2.5.0-SNAPSHOT</springdata.keyvalue>
<springdata.keyvalue>2.5.0-GH-1995-SNAPSHOT</springdata.keyvalue>
<awaitility>4.0.2</awaitility>
<jta>1.1</jta>
<beanutils>1.9.2</beanutils>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,21 @@ public <T> T delete(Object id, String keyspace, Class<T> type) {
*/
@Override
public List<?> getAllOf(String keyspace) {
return getAllOf(keyspace, -1, -1);
return getAllOf(keyspace, Object.class, -1, -1);
}

public List<?> getAllOf(String keyspace, long offset, int rows) {
@Override
public <T> Iterable<T> getAllOf(String keyspace, Class<T> type) {
return getAllOf(keyspace, type, -1, -1);
}

public <T> List<T> getAllOf(String keyspace, Class<T> type, long offset, int rows) {

byte[] binKeyspace = toBytes(keyspace);

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

List<Object> result = new ArrayList<>();
List<T> result = new ArrayList<>();
List<byte[]> keys = new ArrayList<>(ids);

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

for (byte[] key : keys) {
result.add(get(key, keyspace));
result.add(get(key, keyspace, type));
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public <T> Collection<T> execute(RedisOperationChain criteria, Comparator<?> sor
if (criteria == null
|| (CollectionUtils.isEmpty(criteria.getOrSismember()) && CollectionUtils.isEmpty(criteria.getSismember()))
&& criteria.getNear() == null) {
return (Collection<T>) getAdapter().getAllOf(keyspace, offset, rows);
return getAdapter().getAllOf(keyspace, type, offset, rows);
}

RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = connection -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Reference;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.geo.Point;
import org.springframework.data.keyvalue.annotation.KeySpace;
import org.springframework.data.redis.connection.RedisConnection;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class RedisKeyValueAdapterTests {
private RedisKeyValueAdapter adapter;
private StringRedisTemplate template;
private RedisConnectionFactory connectionFactory;
private RedisMappingContext mappingContext;

public RedisKeyValueAdapterTests(RedisConnectionFactory connectionFactory) throws Exception {
this.connectionFactory = connectionFactory;
Expand All @@ -73,7 +75,7 @@ void setUp() {
template = new StringRedisTemplate(connectionFactory);
template.afterPropertiesSet();

RedisMappingContext mappingContext = new RedisMappingContext(
mappingContext = new RedisMappingContext(
new MappingConfiguration(new IndexConfiguration(), new KeyspaceConfiguration()));
mappingContext.afterPropertiesSet();

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

@Test // #1995
void getAllOfShouldReturnSuperTypeIfForUnregisteredTypeAlias() {

Map<String, String> map = new LinkedHashMap<>();
map.put("_class", "taveren");
map.put("address.country", "Andor");
template.opsForHash().putAll("persons:load-1", map);

Object loaded = adapter.get("load-1", "persons", Person.class);

assertThat(loaded).isExactlyInstanceOf(Person.class);
}

@Test // #1995
void getAllOfShouldReturnCorrectTypeIfForRegisteredTypeAlias() {

mappingContext.getPersistentEntity(TaVeren.class);

Map<String, String> map = new LinkedHashMap<>();
map.put("_class", "taveren");
map.put("address.country", "Andor");
template.opsForHash().putAll("persons:load-1", map);

Object loaded = adapter.get("load-1", "persons", Person.class);

assertThat(loaded).isExactlyInstanceOf(TaVeren.class);
}

@Test // DATAREDIS-425
void couldReadsKeyspaceSizeCorrectly() {

Expand Down Expand Up @@ -826,6 +856,7 @@ static class AddressWithPostcode extends Address {
String postcode;
}

@TypeAlias("taveren")
static class TaVeren extends Person {

}
Expand Down