This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fory.git
The following commit(s) were added to refs/heads/main by this push:
new 261de1e6e fix(java): fix set view ref tracking (#3649)
261de1e6e is described below
commit 261de1e6ec4f4325016c7d50bd19db8a0244a04e
Author: Shawn Yang <[email protected]>
AuthorDate: Wed May 6 16:26:41 2026 +0800
fix(java): fix set view ref tracking (#3649)
## Why?
## What does this PR do?
## Related issues
Closes #3645
## AI Contribution Checklist
- [ ] Substantial AI assistance was used in this PR: `yes` / `no`
- [ ] If `yes`, I included a completed [AI Contribution
Checklist](https://github.com/apache/fory/blob/main/AI_POLICY.md#9-contributor-checklist-for-ai-assisted-prs)
in this PR description and the required `AI Usage Disclosure`.
- [ ] If `yes`, my PR description includes the required `ai_review`
summary and screenshot evidence of the final clean AI review results
from both fresh reviewers on the current PR diff or current HEAD after
the latest code changes.
## Does this PR introduce any user-facing change?
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
---
.../collection/CollectionSerializers.java | 2 +-
.../collection/CollectionSerializersTest.java | 63 ++++++++++++++++++++++
2 files changed, 64 insertions(+), 1 deletion(-)
diff --git
a/java/fory-core/src/main/java/org/apache/fory/serializer/collection/CollectionSerializers.java
b/java/fory-core/src/main/java/org/apache/fory/serializer/collection/CollectionSerializers.java
index f76b2286a..beb019655 100644
---
a/java/fory-core/src/main/java/org/apache/fory/serializer/collection/CollectionSerializers.java
+++
b/java/fory-core/src/main/java/org/apache/fory/serializer/collection/CollectionSerializers.java
@@ -574,7 +574,7 @@ public class CollectionSerializers {
int refId = readContext.lastPreservedRefId();
Set set;
if (buffer.readBoolean()) {
- readContext.preserveRefId();
+ readContext.preserveRefId(refId);
set = Collections.newSetFromMap(mapSerializer.newMap(readContext));
setNumElements(mapSerializer.getAndClearNumElements());
} else {
diff --git
a/java/fory-core/src/test/java/org/apache/fory/serializer/collection/CollectionSerializersTest.java
b/java/fory-core/src/test/java/org/apache/fory/serializer/collection/CollectionSerializersTest.java
index 8c6c96ed9..e974b9b82 100644
---
a/java/fory-core/src/test/java/org/apache/fory/serializer/collection/CollectionSerializersTest.java
+++
b/java/fory-core/src/test/java/org/apache/fory/serializer/collection/CollectionSerializersTest.java
@@ -28,6 +28,10 @@ import static org.testng.Assert.assertSame;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.AbstractCollection;
@@ -906,6 +910,65 @@ public class CollectionSerializersTest extends
ForyTestBase {
}
}
+ @Test
+ public void testSetFromMapNestedInExternalizablePreservesRefIds() {
+ Fory fory =
+ Fory.builder()
+ .withXlang(false)
+ .withRefTracking(true)
+ .withCodegen(false)
+ .requireClassRegistration(true)
+ .suppressClassRegistrationWarnings(true)
+ .build();
+ fory.register(ExternalizableSetFromMapHolder.class);
+ fory.register(RefMarker.class);
+
+ Set<String> set = Collections.newSetFromMap(new ConcurrentHashMap<>());
+ RefMarker marker = new RefMarker("after-set");
+ ExternalizableSetFromMapHolder holder = new
ExternalizableSetFromMapHolder(set, marker, marker);
+
+ ExternalizableSetFromMapHolder deserialized = serDe(fory, holder);
+ assertSame(deserialized.after, deserialized.afterAgain);
+ }
+
+ public static final class ExternalizableSetFromMapHolder implements
Externalizable {
+ private Set<?> set;
+ private RefMarker after;
+ private RefMarker afterAgain;
+
+ public ExternalizableSetFromMapHolder() {}
+
+ private ExternalizableSetFromMapHolder(Set<?> set, RefMarker after,
RefMarker afterAgain) {
+ this.set = set;
+ this.after = after;
+ this.afterAgain = afterAgain;
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(set);
+ out.writeObject(after);
+ out.writeObject(afterAgain);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
+ set = (Set<?>) in.readObject();
+ after = (RefMarker) in.readObject();
+ afterAgain = (RefMarker) in.readObject();
+ }
+ }
+
+ public static final class RefMarker {
+ private String value;
+
+ public RefMarker() {}
+
+ private RefMarker(String value) {
+ this.value = value;
+ }
+ }
+
@Test(dataProvider = "foryCopyConfig")
public void testSetFromMapCopy(Fory fory) {
final Set<Object> set = Collections.newSetFromMap(Maps.newConcurrentMap());
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]