spring-projects / spring-projects/spring-data-redis

MappingRedisConverter.writeCollection breaks on NULL values and missing next elements [DATAREDIS-1076]

Open
#1,648 2 comments 0 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Dec 30, 2020.

in: core type: bug
Dominant language
Java
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

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: https://github.com/spring-projects/spring-data-redis/commit/2492fbe332b7565e0724b8f7ec6c62f5d3178b25#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 https://github.com/spring-projects/spring-data-redis/commit/2492fbe332b7565e0724b8f7ec6c62f5d3178b25#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)

Reference URL: https://github.com/binakot/spring-data-redis-null-elements/blob/master/src/test/java/com/baeldung/spring/data/redis/repo/NullableCollectionRepositoryIntegrationTest.java#L50

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.