Skip to content

DATAREDIS-509 - Fix handling of primitive arrays in MappingRedisConverter. #196

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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.0.BUILD-SNAPSHOT</version>
<version>1.8.0.DATAREDIS-509-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -426,7 +426,7 @@ public void doWithPersistentProperty(KeyValuePersistentProperty persistentProper
persistentProperty.getTypeInformation().getComponentType(), sink);
} else if (property.getClass().isArray()) {

writeCollection(keyspace, propertyStringPath, Arrays.asList((Object[]) property),
writeCollection(keyspace, propertyStringPath, CollectionUtils.arrayToList(property),
persistentProperty.getTypeInformation().getComponentType(), sink);
} else {

Expand Down Expand Up @@ -592,7 +592,7 @@ private Object readCollectionOrArray(String path, Class<?> collectionType, Class
}
}

return isArray ? target.toArray((Object[]) Array.newInstance(valueType, target.size())) : target;
return isArray ? toArray(target, collectionType, valueType) : target;
}

/**
Expand Down Expand Up @@ -749,6 +749,30 @@ public <T> T fromBytes(byte[] source, Class<T> type) {
return conversionService.convert(source, type);
}

/**
* Converts a given {@link Collection} into an array considering primitive types.
*
* @param source {@link Collection} of values to be added to the array.
* @param arrayType {@link Class} of array.
* @param valueType to be used for conversion before setting the actual value.
* @return
*/
private Object toArray(Collection<Object> source, Class<?> arrayType, Class<?> valueType) {

if (!ClassUtils.isPrimitiveArray(arrayType)) {
return source.toArray((Object[]) Array.newInstance(valueType, source.size()));
}

Object targetArray = Array.newInstance(valueType, source.size());
Iterator<Object> iterator = source.iterator();
int i = 0;
while (iterator.hasNext()) {
Array.set(targetArray, i, conversionService.convert(iterator.next(), valueType));
i++;
}
return targetArray;
}

/**
* Set {@link CustomConversions} to be applied.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
package org.springframework.data.redis.core.convert;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
Expand All @@ -39,6 +37,7 @@
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
* {@link IndexResolver} implementation considering properties annotated with {@link Indexed} or paths set up in
Expand Down Expand Up @@ -124,17 +123,18 @@ public void doWithPersistentProperty(KeyValuePersistentProperty persistentProper
if (Iterable.class.isAssignableFrom(propertyValue.getClass())) {
iterable = (Iterable) propertyValue;
} else if (propertyValue.getClass().isArray()) {
iterable = Arrays.asList((Object[]) propertyValue);
iterable = CollectionUtils.arrayToList(propertyValue);
} else {
throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type of collection");
throw new RuntimeException(
"Don't know how to handle " + propertyValue.getClass() + " type of collection");
}

for (Object listValue : iterable) {

if (listValue != null) {
TypeInformation<?> typeToUse = updateTypeHintForActualValue(typeHint, listValue);
indexes.addAll(
doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue));
indexes.addAll(doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty,
listValue));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public static class WithArrays {
Object[] arrayOfObject;
String[] arrayOfSimpleTypes;
Species[] arrayOfCompexTypes;
int[] arrayOfPrimitives;
}

static class TypeWithObjectValueTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,36 @@ public void shouldWriteReadObjectListValueTypeCorrectly() {
assertThat(result.list.get(2), instanceOf(Date.class));
}

/**
* @see DATAREDIS-509
*/
@Test
public void writeHandlesArraysOfPrimitivesProperly() {

Map<String, String> source = new LinkedHashMap<String, String>();
source.put("arrayOfPrimitives.[0]", "1");
source.put("arrayOfPrimitives.[1]", "2");
source.put("arrayOfPrimitives.[2]", "3");

WithArrays target = read(WithArrays.class, source);

assertThat(target.arrayOfPrimitives[0], is(1));
assertThat(target.arrayOfPrimitives[1], is(2));
assertThat(target.arrayOfPrimitives[2], is(3));
}

/**
* @see DATAREDIS-509
*/
@Test
public void readHandlesArraysOfPrimitivesProperly() {

WithArrays source = new WithArrays();
source.arrayOfPrimitives = new int[] { 1, 2, 3 };
assertThat(write(source).getBucket(), isBucket().containingUtf8String("arrayOfPrimitives.[0]", "1")
.containingUtf8String("arrayOfPrimitives.[1]", "2").containingUtf8String("arrayOfPrimitives.[2]", "3"));
}

private RedisData write(Object source) {

RedisData rdo = new RedisData();
Expand Down