Description
binakot opened DATAREDIS-1076 and commented
Good day!
I'm using spring-data-redis
to store some collections with nullable elements. There are cases when some elements may be null. I was surpriced when I found a losing of ending elements in my collections.
I did a search the reason, and I found it: 2492fbe#diff-b71f4f4366952781ed005f6224ff7d5dR511
This break
is still in the project https://github.com/spring-projects/spring-data-redis/blob/master/src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java#L705
Why is it required to break the writing elements on the first null in MappingRedisConverter.writeCollection
? And why it didn't required before this commit 2492fbe#diff-b71f4f4366952781ed005f6224ff7d5dR511?
I made a project with sample of the problem with different cases: leading/ending nulls and null in a middle of collections. https://github.com/binakot/spring-data-redis-null-elements/blob/master/src/test/java/com/baeldung/spring/data/redis/repo/NullableCollectionRepositoryIntegrationTest.java#L51
I'm using spring-boot
version 1.5.22.RELEASE
in my application, but this behaviour is repeatable with version 2.1.9.RELEASE
.
Redis stores the elements in separate values this way:
java:
Integer[] collection = {1,2,3,4,5};
redis:
collection.[0]: 1
collection.[1]: 2
collection.[2]: 3
collection.[3]: 4
collection.[4]: 5
When some element is null, Redis just skips the elements:
java:
Integer[] collection = {1,2,3,null,null};
redis:
collection.[0]: 1
collection.[1]: 2
collection.[2]: 3
But when nulls are not in the end:
java:
Integer[] collection = {1,2,null,4,5};
redis:
collection.[0]: 1
collection.[1]: 2
... lost 4 and 5
I'm using array, that's why I get null elements, if Redis cannot find value with certain index. And it's a behaviour which I excpect. At this moment I'm losing any values after the first null in an array. Behaviour of List and Array are similar, here is same tests for array of Integers https://github.com/binakot/spring-data-redis-null-elements/blob/master/src/test/java/com/baeldung/spring/data/redis/repo/NullableArrayRepositoryIntegrationTest.java#L50
What can I do? I cannot use some pseudo-null like Integer.MIN_VALUE or smth else.
Affects: 2.2.3 (Moore SR3)