-
Notifications
You must be signed in to change notification settings - Fork 843
Closed
Labels
Description
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