Fix ReflectionCache to be serializable by k163377 · Pull Request #634 · FasterXML/jackson-module-kotlin (original) (raw)

@k163377 There are existing tests that can help. json-core f.ex has:

https://github.com/FasterXML/jackson-core/blob/2.15/src/test/java/com/fasterxml/jackson/core/TestJDKSerializability.java

so you need not write a file, but just write as byte[]/read from byte[]. Helper methods like:

    protected byte[] jdkSerialize(Object o) throws IOException
    {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);
        ObjectOutputStream obOut = new ObjectOutputStream(bytes);
        obOut.writeObject(o);
        obOut.close();
        return bytes.toByteArray();
    }

    protected <T> T jdkDeserialize(byte[] raw) throws IOException
    {
        ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw));
        try {
            return (T) objIn.readObject();
        } catch (ClassNotFoundException e) {
            fail("Missing class: "+e.getMessage());
            return null;
        } finally {
            objIn.close();
        }
    }

and with that I think 2 things need to be tested, if not already:

  1. ObjectMapper with Kotlin module registered (so that everything needed is java.io.Serializabe and
  2. ReflectionCache itself

Of these (1) might fail, and figuring out which things it refers to are not serializable can be some work. So here, just adding test for (2) would make sense, I think.

... although there is also difference between empty cache with no entries (not referencing things that might not be Serializable) and cache with entries. This is why test (1) is often done only after serializing once, deserializing once, to let referenced components be initialized.

But on verifying that a class is JDK serializable/deserializable, all that is needed is for write-then-read to succeed without exception.

I hope this helps.