Skip to content

Copy the property values in BeanSerializer.copy - #1535

Merged
theigl merged 1 commit into
EsotericSoftware:masterfrom
CCCLL:fix-bean-serializer-copy
Oct 21, 2026
Merged

theigl merged 1 commit into
EsotericSoftware:masterfrom
CCCLL:fix-bean-serializer-copy

Conversation

@CCCLL

@CCCLL CCCLL commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #1534.

BeanSerializer.copy set the property values of the original on the copy, so Kryo.copy returned a shallow copy of a bean: the copy shared mutable property values with the original, and a property that referenced the bean referenced the original instead of the copy.

The copy is now referenced with Kryo.reference before the property values are copied with Kryo.copy, like in FieldSerializer.copy. With Kryo.copyShallow, Kryo.copy returns the property values as they are, so a shallow copy is unchanged. Like with FieldSerializer, a deep copy now needs the classes of the property values to be registered if registration is required.

BeanSerializerTest.testCopy checks that a copy doesn't share a property value with the original, that a property referencing the bean references the copy, and that a shallow copy still shares the property values. It fails without the fix, and with either change alone: without Kryo.copy the property value is shared, and without Kryo.reference copying the bean that references itself overflows the stack.

mvn -pl main test passes on JDK 8 (built with JDK 11), 11, 17, 21 and 25, and mvn -pl main formatter:validate passes. With the change applied to kryo-6, its tests pass on JDK 17, 21 and 25.

BeanSerializer.copy set the property values of the original on the copy,
so Kryo.copy returned a shallow copy of a bean: the copy shared mutable
property values with the original, and a property that referenced the
bean referenced the original instead of the copy.

The copy is now referenced before the property values are copied with
Kryo.copy, like in FieldSerializer.copy. With Kryo.copyShallow, the
property values are still set as they are.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved issues were identified, and all reviewers assessed it as ready.

No open issues 🎉

What changed in this PR

Fixes BeanSerializer.copy to deep-copy bean properties while preserving self-references and shallow-copy behavior.

Changes:

  • References the copied bean before copying properties.
  • Deep-copies property values with Kryo.copy.
  • Adds regression coverage for deep, circular, and shallow copies.
File Description
test/​com/​esotericsoftware/​kryo/​serializers/​BeanSerializerTest.java Tests deep-copy isolation, self-references, and shallow copies.
src/​com/​esotericsoftware/​kryo/​serializers/​BeanSerializer.java Corrects bean property copying and reference handling.

Review effort: Lite

@theigl
theigl merged commit 054a581 into EsotericSoftware:master Oct 21, 2026
1 check passed
@theigl

theigl commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Thanks again @CCCLL!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

BeanSerializer.copy makes a shallow copy

3 participants