DATAREDIS-544 - Read constructor property values through MappingRedisConverter.
We now read and convert constructor property values through MappingRedisConverter.readProperty(…) to apply proper conversion. This way, constructor properties are read through the same methods as instance properties allowing to read back nested level entities. Previously, we were only reading simple properties or properties associated with a registered Converter. Original Pull Request: #329
This commit is contained in:
committed by
Christoph Strobl
parent
9ede7b317e
commit
58d7ff90a5
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core.convert;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -84,6 +85,24 @@ public class ConversionTestEntities {
|
||||
Species species;
|
||||
}
|
||||
|
||||
@RedisHash(KEYSPACE_PERSON)
|
||||
@Data
|
||||
public static class RecursiveConstructorPerson {
|
||||
|
||||
final @Id String id;
|
||||
final String firstname;
|
||||
final RecursiveConstructorPerson father;
|
||||
String lastname;
|
||||
}
|
||||
|
||||
@RedisHash(KEYSPACE_PERSON)
|
||||
@Data
|
||||
public static class PersonWithConstructorAndAddress {
|
||||
|
||||
final @Id String id;
|
||||
final Address address;
|
||||
}
|
||||
|
||||
public static class PersonWithAddressReference extends Person {
|
||||
|
||||
@Reference AddressWithId addressRef;
|
||||
|
||||
@@ -231,6 +231,31 @@ public class MappingRedisConverterUnitTests {
|
||||
assertThat(target.address, instanceOf(AddressWithPostcode.class));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-544
|
||||
public void readEntityViaConstructor() {
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("id", "bart");
|
||||
map.put("firstname", "Bart");
|
||||
map.put("lastname", "Simpson");
|
||||
|
||||
map.put("father.id", "homer");
|
||||
map.put("father.firstname", "Homer");
|
||||
map.put("father.lastname", "Simpson");
|
||||
|
||||
RecursiveConstructorPerson target = converter.read(RecursiveConstructorPerson.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(map)));
|
||||
|
||||
assertThat(target.id, is("bart"));
|
||||
assertThat(target.firstname, is("Bart"));
|
||||
assertThat(target.lastname, is("Simpson"));
|
||||
assertThat(target.father, is(notNullValue()));
|
||||
assertThat(target.father.id, is("homer"));
|
||||
assertThat(target.father.firstname, is("Homer"));
|
||||
assertThat(target.father.lastname, is("Simpson"));
|
||||
assertThat(target.father.father, is(nullValue()));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-425
|
||||
public void writeAddsClassTypeInformationCorrectlyForNonMatchingTypesInCollections() {
|
||||
|
||||
@@ -1093,6 +1118,25 @@ public class MappingRedisConverterUnitTests {
|
||||
assertThat(target.address.country, is("Tel'aran'rhiod"));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-544
|
||||
public void readShouldHonorCustomConversionOnNestedTypeViaConstructorCreation() {
|
||||
|
||||
this.converter = new MappingRedisConverter(new RedisMappingContext(), null, resolverMock);
|
||||
this.converter
|
||||
.setCustomConversions(new RedisCustomConversions(Collections.singletonList(new BytesToAddressConverter())));
|
||||
this.converter.afterPropertiesSet();
|
||||
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
map.put("address", "{\"city\":\"unknown\",\"country\":\"Tel'aran'rhiod\"}");
|
||||
|
||||
PersonWithConstructorAndAddress target = converter.read(PersonWithConstructorAndAddress.class,
|
||||
new RedisData(Bucket.newBucketFromStringMap(map)));
|
||||
|
||||
assertThat(target.address, notNullValue());
|
||||
assertThat(target.address.city, is("unknown"));
|
||||
assertThat(target.address.country, is("Tel'aran'rhiod"));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-425
|
||||
public void writeShouldPickUpTimeToLiveFromPropertyIfPresent() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user