Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record serialization uses incorrect types #847

Closed
purpledrazi opened this issue Jul 28, 2021 · 2 comments · Fixed by #848
Closed

Record serialization uses incorrect types #847

purpledrazi opened this issue Jul 28, 2021 · 2 comments · Fixed by #848
Labels

Comments

@purpledrazi
Copy link
Contributor

Describe the bug
Record fields are serialized with the wrong type. It appears that Kryo is using the declared type of the field rather than the actual type of the object assigned to the field.

To Reproduce
The code below fails with the error "Class is not registered: java.lang.Number".

Registering java.lang.Number allows the serialization to succeed, but then deserialization fails because it attempts to create an instance of Number.

import java.nio.ByteBuffer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.ByteBufferInput;
import com.esotericsoftware.kryo.io.ByteBufferOutput;

public class KyroRecordTest {

    public static void main(String[] args) {
        var kryo = new Kryo();
        kryo.register(ExampleRecord.class);

        ByteBuffer buf = ByteBuffer.allocate(1000);

        kryo.writeObject(new ByteBufferOutput(buf), new ExampleRecord(12345));
        buf.flip();

        Object o = kryo.readObject(new ByteBufferInput(buf), ExampleRecord.class);

        System.out.println(o);

    }

    public static record ExampleRecord(Number n) {}
}

Environment:

  • OS: Debian
  • JDK Version: 16.0.2
  • Kryo Version: 5.1.1
@theigl
Copy link
Collaborator

theigl commented Jul 28, 2021

@purpledrazi: Thanks for this bug report!

This is quite an obvious bug. It seems nobody has tried serializing non-trivial records so far.

I'll look into this as soon as I can and hope I can fix it without breaking serialization compatibility.

@theigl
Copy link
Collaborator

theigl commented Jul 29, 2021

@purpledrazi: I have created a PR for this issue. Unfortunately, it is not possible to solve this in a fully backwards-compatible way. But since this bug makes the record serializer basically unusable in real world applications, I decided to break compatibility and allow existing users to opt-in to the old behavior.

The fix will be part of Kryo 5.2.0, which I plan to release in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants