Skip to content
New issue

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

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

Already on GitHub? # to your account

ExternalizableSerializer does not support circular references #1094

Open
mikehearn opened this issue Jun 21, 2024 · 2 comments
Open

ExternalizableSerializer does not support circular references #1094

mikehearn opened this issue Jun 21, 2024 · 2 comments

Comments

@mikehearn
Copy link

Describe the bug

If an object implements Externalizable and is registered with the ExternalizableSerializer, there is no way for it to call kryo.reference() on itself and the ExternalizableSerializer doesn't do this either. As a consequence, if the Externalizable attempts to read a reference to itself, it will get null and deserialize incorrectly.

To Reproduce

Provide a minimal reproducible example of the problem, ideally in the form of a runnable test-case.

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.ExternalizableSerializer;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class ExternalizableBugProof {
    static class Example implements Externalizable {

        @Override
        public void writeExternal(ObjectOutput out) throws IOException {
            out.writeUTF("abc");
            out.writeObject(this);
        }

        @Override
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            in.readUTF();
            Object obj = in.readObject();
            assert obj == this : "" + obj;
        }
    }

    public static void main(String[] args) {
        var kryo = new Kryo();
        kryo.setReferences(true);
        kryo.setRegistrationRequired(false);
        kryo.addDefaultSerializer(ExternalizableBugProof.Example.class, ExternalizableSerializer.class);

        var output = new Output(1024);
        kryo.writeObject(output, new Example());
        kryo.readObject(new Input(output.getBuffer()), Example.class);
    }
}

Environment:

Kryo 5.6.0

@theigl
Copy link
Collaborator

theigl commented Jun 24, 2024

The ExternalizableSerializer has not been touched in a very long time. I'm guessing that not many people are using it. What use case do you have that prevents you from using FieldSerializer or KryoSerializable?

@mikehearn
Copy link
Author

An object that wants to define its own serialization format independent of Kryo so it can ensure backwards compatibility as it evolves over time.

# for free to join this conversation on GitHub. Already have an account? # to comment
Development

No branches or pull requests

2 participants