JDK-8237202 : Discuss evolution of records in serialization spec
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.io:serialization
  • Priority: P2
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 14
  • Submitted: 2020-01-15
  • Updated: 2020-07-01
  • Resolved: 2020-01-21
Related Reports
CSR :  
Relates :  
Description
Summary
-------

Describe allowable types of evolution compatible changes to serializable
record classes.

Problem
-------

The record-specific Java Object Serialization specification is silent on
the types of changes that can be made to record classes while
maintaining compatibility with preview serial versions.

Solution
--------

In subsection 5.6.2 - "Compatible Changes", add allowable types of
evolution compatible changes that can be made to serializable record
classes.

Specification
-------------

### 5.6.2 Compatible Changes

    ...
    -   Adding or removing a record component - When the record object being
        reconstituted has a record component that does not occur in the stream, the
        record class's canonical constructor will be passed the default value for
        its type. If specific initialization is needed, the constructor can
        initialize the component to a non-default value. Stream field values not
        passed to the canonical constructor are effectively discarded.

    -   Changing a class from an ordinary class to a record class - A class that is
        suitable to be converted from an ordinary class to a record class, and
        relies on default serialization, may be changed to a record class. The
        ordinary class should have `java.lang.Object` as its direct superclass, or
        otherwise have no serializable state in its superclasses. The name and type
        of the record class's components must match that of the name and type of the
        ordinary class's serializable fields. Record objects are reconstructed
        through the record class's canonical constructor. If the canonical
        constructor throws an exception, say while checking invariants, then an
        `InvalidObjectException` is thrown.

    -   Changing a class from a record class to an ordinary class - A record class
        that relies on default serialization, may be changed to an ordinary class.
        The ordinary class must declare an explicit `serialVersionUID`, whose value
        is the same as that of the prior record class's `serialVersionUID`, or
        `0L` if the prior record class does not have an explicit `serialVersionUID`
        declaration. The name and type of the ordinary class's serializable fields
        must match that of the name and type of the prior record class's components.

Viewable online at:
  https://cr.openjdk.java.net/~chegar/records/spec/records-serialization-addendum.html

Comments
Moving amended request to Approved.
21-01-2020

That is correct Joe, good idea. Withdraw - edit - re-finalize
19-01-2020

For The ordinary class must declare an explicit `serialVersionUID`, whose value is the same as that of the prior record class's `serialVersionUID`. it may be worth explicitly stating "so if the record doesn't have an explicit serialVersionUID, the class should declare a serialVersionUID with a value of 0," assuming that is indeed the case.
18-01-2020