SLING-11466 : JcrPropertyMapCacheEntry: ValueFormatException when converting value InputStream to number-array
diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java b/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
index 6aac379..dcc2e02 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntry.java
@@ -223,10 +223,10 @@
}
} else {
-
+ // source is not multivalued
final Object sourceObject = this.getPropertyValue();
if (targetIsArray) {
- result = (T) convertToArray(new Object[]{sourceObject}, type.getComponentType(), node, dynamicClassLoader);
+ result = (T) convertToArray(sourceObject, type.getComponentType(), node, dynamicClassLoader);
} else {
result = convertToType(-1, sourceObject, type, node, dynamicClassLoader);
}
@@ -242,6 +242,21 @@
return result;
}
+ private @NotNull<T> T[] convertToArray(final @NotNull Object source,
+ final @NotNull Class<T> type,
+ final @NotNull Node node,
+ final @Nullable ClassLoader dynamicClassLoader) throws RepositoryException {
+ List<T> values = new ArrayList<>();
+ T value = convertToType(-1, source, type, node, dynamicClassLoader);
+ if (value != null) {
+ values.add(value);
+ }
+
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(type, values.size());
+ return values.toArray(result);
+ }
+
private @NotNull<T> T[] convertToArray(final @NotNull Object[] sourceArray,
final @NotNull Class<T> type,
final @NotNull Node node,
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
index 90ff8d8..53d2d44 100644
--- a/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/helper/JcrPropertyMapCacheEntryTest.java
@@ -185,11 +185,12 @@
Double[] result = entry.convertToType(Double[].class, node, null);
assertNotNull(result);
assertEquals(1, result.length);
- assertEquals(Double.valueOf(10.7), result[0]);
+ assertEquals(Double.valueOf(4.0), result[0]);
verify(prop, times(2)).isMultiple();
verify(prop).getValue();
verify(prop).getType();
+ verify(prop).getLength();
verifyNoMoreInteractions(prop);
verifyZeroInteractions(node);
}