EsotericSoftware/kryo

java.util.ImmutableCollections$ListN can contain null values

開放

#1,239 建立於 2025年12月5日

 (1 則留言) (0 個反應) (0 位負責人)HTML (839 個分叉)batch import
bughelp wanted

倉庫指標

星標
 (6,545 顆星)
PR 合併指標
 (平均合併 107天 5小時) (30 天內合併 9 個 PR)

描述

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)

  public void testImmutableDeserialization() throws Exception {
    final Kryo kryo = new Kryo();
    kryo.setRegistrationRequired(false);
    final Output output = new Output(64);
    final List<Integer> 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<Integer> 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.

貢獻者指南