Skip to content

Record serialization uses incorrect types #847

@purpledrazi

Description

@purpledrazi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions