chaokunyang commented on PR #3902:
URL: https://github.com/apache/fory/pull/3902#issuecomment-5151631926
After reviewing this issue, cyclic references should not be used as `Set`
elements or `Map` keys. Fory must publish an object reference before all of its
fields have been fully deserialized in order to resolve cycles. If that
partially materialized object is inserted into a hash-based container,
`hashCode()` or `equals()` may observe incomplete state. When the remaining
fields are restored, the hash may change and invalidate the container's
internal structure.
This happened to work previously because the relevant `List` or collection
field was serialized and deserialized last, after the fields used by
`hashCode()` and `equals()` had already been restored. That ordering was
incidental and is no longer guaranteed after the field-ordering semantics were
adjusted.
The recommended workaround is to serialize the cyclic references as a
`List`, mark the derived `Set` as `transient`, and materialize the `Set` lazily
after deserialization:
```java
private List<Node> parentList = new ArrayList<>();
private transient Set<Node> parents;
public Set<Node> getParents() {
if (parents == null) {
parents = new LinkedHashSet<>(parentList);
}
return parents;
}
```
The same approach can be used for maps by serializing an ordered list of
key-value entries and constructing the map after the object graph has been
fully deserialized. Lists, arrays, object fields, and map values may still
contain cyclic references; the limitation applies specifically to cyclic
objects used as set elements or map keys.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]