EsotericSoftware / EsotericSoftware/kryo

java.util.ImmutableCollections$ListN can contain null values

Open
#1,239 1 comment 0 reactions 0 assignees View on GitHub
bug help wanted
Dominant language
HTML
Stars
6.5k
Forks
841
Avg merge
39m
Merged PRs (30d)
4

Description

**Describe the bug**
In older java versions, `List::of` was the only way to create a java.util.ImmutableCollections$ListN object, but now `Stream::toList` is also capable of creating them, and it does not have the limitation of not containing nulls

**To Reproduce**
There are two slightly different issues. It should be possible to fix both at the same time.
In this case, immutableWithNulls only has null values in it, so kryo successfully serializes it, but then can not deserialize it because it ends up calling `List.of(null, null)`
``` @Test
public void testImmutableDeserialization() throws Exception {
final Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
final Output output = new Output(64);
final List immutableWithNulls = Stream.of(null,1, null).filter(x -> x == null || x > 10).toList();

System.out.println(immutableWithNulls.getClass());
kryo.writeClassAndObject(output, immutableWithNulls);
try(Input input = new Input(output.getBuffer())) {
final Object copy = kryo.readClassAndObject(input);
assert immutableWithNulls.equals(copy);
}
```

In this case, immutableWithNulls has both null and non-null values, so kryo catches that there are null values in a supposedly nonnull container during serialization
```
}
@Test
public void testImmutableSerialization() throws Exception {
final Kryo kryo = new Kryo();
kryo.setRegistrationRequired(false);
final Output output = new Output(64);
final List immutableWithNulls = Stream.of(null,1, null).toList();
kryo.writeClassAndObject(output, immutableWithNulls);
try(Input input = new Input(output.getBuffer())) {
final Object copy = kryo.readClassAndObject(input);
assert immutableWithNulls.equals(copy);
}
```
**Environment:**
- OS: Ubuntu running in WSL
- JDK 21 (but Stream::toList was added in 16. I have no clue if that is when it started returning a ListN)
- Kryo 5.5

**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.