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 75d0915cc fix(java): fix register by id for xlang metashare mode in 
java  (#2614)
75d0915cc is described below

commit 75d0915cc2c6cd88dd85f05917093d3612a9d9f3
Author: Shawn Yang <[email protected]>
AuthorDate: Tue Sep 16 13:29:33 2025 +0800

    fix(java): fix register by id for xlang metashare mode in java  (#2614)
    
    ## Why?
    
    fix register by id for xlang metashare mode in java
    
    ## What does this PR do?
    
    <!-- Describe the details of this PR. -->
    
    ## Related issues
    
    <!--
    Is there any related issue? If this PR closes them you say say
    fix/closes:
    
    - #xxxx0
    - #xxxx1
    - Fixes #xxxx2
    -->
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fory/issues/new/choose) describing the
    need to do so and update the document if necessary.
    
    Delete section if not applicable.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    
    Delete section if not applicable.
    -->
---
 .../java/org/apache/fory/meta/TypeDefEncoder.java  |  2 +-
 .../org/apache/fory/resolver/XtypeResolver.java    | 26 +++++++-----------
 .../java/org/apache/fory/CrossLanguageTest.java    | 32 +++++++++++++++++++---
 python/pyfory/tests/test_cross_language.py         | 13 ++++++++-
 4 files changed, 51 insertions(+), 22 deletions(-)

diff --git 
a/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java 
b/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
index 69b87bfa6..3a477a78b 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
@@ -103,7 +103,7 @@ class TypeDefEncoder {
       buffer.writeVarUint32(fields.size() - SMALL_NUM_FIELDS_THRESHOLD);
     }
     if (resolver.isRegisteredById(type)) {
-      buffer.writeVarUint32(xtypeId);
+      buffer.writeVarUint32(classInfo.getXtypeId());
     } else {
       Preconditions.checkArgument(resolver.isRegisteredByName(type));
       currentClassHeader |= REGISTER_BY_NAME_FLAG;
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java 
b/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java
index 6c25e9060..fb51af749 100644
--- a/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java
+++ b/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java
@@ -74,7 +74,6 @@ import org.apache.fory.reflect.TypeRef;
 import org.apache.fory.serializer.ArraySerializers;
 import 
org.apache.fory.serializer.DeferedLazySerializer.DeferedLazyObjectSerializer;
 import org.apache.fory.serializer.EnumSerializer;
-import org.apache.fory.serializer.LazySerializer;
 import org.apache.fory.serializer.NonexistentClass;
 import org.apache.fory.serializer.NonexistentClassSerializers;
 import org.apache.fory.serializer.ObjectSerializer;
@@ -182,14 +181,15 @@ public class XtypeResolver extends TypeResolver {
     if (type.isEnum()) {
       xtypeId = (xtypeId << 8) + Types.ENUM;
     } else {
+      int id = (xtypeId << 8) + (shareMeta ? Types.COMPATIBLE_STRUCT : 
Types.STRUCT);
       if (serializer != null) {
         if (isStructType(serializer)) {
-          xtypeId = (xtypeId << 8) + Types.STRUCT;
+          xtypeId = id;
         } else {
           xtypeId = (xtypeId << 8) + Types.EXT;
         }
       } else {
-        xtypeId = (xtypeId << 8) + Types.STRUCT;
+        xtypeId = id;
       }
     }
     register(
@@ -303,7 +303,7 @@ public class XtypeResolver extends TypeResolver {
     if (serializer instanceof ObjectSerializer || serializer instanceof 
GeneratedSerializer) {
       return true;
     }
-    return serializer instanceof LazySerializer.LazyObjectSerializer;
+    return serializer instanceof DeferedLazyObjectSerializer;
   }
 
   private ClassInfo newClassInfo(Class<?> type, Serializer<?> serializer, 
short xtypeId) {
@@ -365,10 +365,7 @@ public class XtypeResolver extends TypeResolver {
     if (classInfo == null) {
       return false;
     }
-    byte xtypeId = (byte) classInfo.xtypeId;
-    if (xtypeId <= 0) {
-      return false;
-    }
+    int xtypeId = classInfo.xtypeId & 0xff;
     switch (xtypeId) {
       case Types.NAMED_COMPATIBLE_STRUCT:
       case Types.NAMED_ENUM:
@@ -386,10 +383,7 @@ public class XtypeResolver extends TypeResolver {
     if (classInfo == null) {
       return false;
     }
-    byte xtypeId = (byte) classInfo.xtypeId;
-    if (xtypeId <= 0) {
-      return false;
-    }
+    int xtypeId = classInfo.xtypeId & 0xff;
     switch (xtypeId) {
       case Types.NAMED_COMPATIBLE_STRUCT:
       case Types.NAMED_ENUM:
@@ -466,7 +460,7 @@ public class XtypeResolver extends TypeResolver {
   }
 
   public ClassInfo getUserTypeInfo(int userTypeId) {
-    Preconditions.checkArgument((byte) (userTypeId) < Types.UNKNOWN);
+    Preconditions.checkArgument((userTypeId & 0xff) < Types.UNKNOWN);
     return xtypeIdToClassMap.get(userTypeId);
   }
 
@@ -623,7 +617,7 @@ public class XtypeResolver extends TypeResolver {
   @Override
   public void writeClassInfo(MemoryBuffer buffer, ClassInfo classInfo) {
     int xtypeId = classInfo.getXtypeId();
-    byte internalTypeId = (byte) xtypeId;
+    int internalTypeId = xtypeId & 0xff;
     buffer.writeVarUint32Small7(xtypeId);
     switch (internalTypeId) {
       case Types.NAMED_ENUM:
@@ -716,8 +710,8 @@ public class XtypeResolver extends TypeResolver {
   }
 
   public ClassInfo readClassInfo(MemoryBuffer buffer) {
-    long xtypeId = buffer.readVarUint32Small14();
-    byte internalTypeId = (byte) xtypeId;
+    int xtypeId = buffer.readVarUint32Small14();
+    int internalTypeId = xtypeId & 0xff;
     switch (internalTypeId) {
       case Types.NAMED_ENUM:
       case Types.NAMED_STRUCT:
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java 
b/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java
index 17b0cba60..85f6ed17a 100644
--- a/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java
@@ -501,11 +501,13 @@ public class CrossLanguageTest extends ForyTestBase {
     structRoundBack(fory, obj2, "test_serialize_simple_struct" + (compatible ? 
"_compatible" : ""));
   }
 
-  @Test
-  public void testRegisterById() throws Exception {
+  @Test(dataProvider = "compatible")
+  public void testRegisterById(boolean compatible) throws Exception {
     Fory fory =
         Fory.builder()
             .withLanguage(Language.XLANG)
+            .withCompatibleMode(
+                compatible ? CompatibleMode.COMPATIBLE : 
CompatibleMode.SCHEMA_CONSISTENT)
             .withRefTracking(true)
             .requireClassRegistration(false)
             .build();
@@ -513,7 +515,7 @@ public class CrossLanguageTest extends ForyTestBase {
     ComplexObject2 obj2 = new ComplexObject2();
     obj2.f1 = true;
     obj2.f2 = new HashMap<>(ImmutableMap.of((byte) -1, 2));
-    structRoundBack(fory, obj2, "test_register_by_id");
+    structRoundBack(fory, obj2, "test_register_by_id" + (compatible ? 
"_compatible" : ""));
   }
 
   @Test(dataProvider = "enableCodegen")
@@ -573,7 +575,7 @@ public class CrossLanguageTest extends ForyTestBase {
     System.out.println(dataFile.toAbsolutePath());
     Files.deleteIfExists(dataFile);
     Files.write(dataFile, serialized);
-    // dataFile.toFile().deleteOnExit();
+    dataFile.toFile().deleteOnExit();
     ImmutableList<String> command =
         ImmutableList.of(
             PYTHON_EXECUTABLE, "-m", PYTHON_MODULE, testName, 
dataFile.toAbsolutePath().toString());
@@ -861,6 +863,28 @@ public class CrossLanguageTest extends ForyTestBase {
     structRoundBack(fory, a, "test_enum_field" + (compatible ? "_compatible" : 
""));
   }
 
+  @Test(dataProvider = "compatible")
+  public void testEnumFieldRegisterById(boolean compatible) throws 
java.io.IOException {
+    Fory fory =
+        Fory.builder()
+            // avoid generated code conflict with register by name
+            .withName("testEnumFieldRegisterById")
+            .withLanguage(Language.XLANG)
+            .withCompatibleMode(
+                compatible ? CompatibleMode.COMPATIBLE : 
CompatibleMode.SCHEMA_CONSISTENT)
+            .requireClassRegistration(true)
+            .build();
+    fory.register(EnumTestClass.class, 1);
+    fory.register(EnumFieldStruct.class, 2);
+
+    EnumFieldStruct a = new EnumFieldStruct();
+    a.f1 = EnumTestClass.FOO;
+    a.f2 = EnumTestClass.BAR;
+    a.f3 = "abc";
+    Assert.assertEquals(xserDe(fory, a), a);
+    structRoundBack(fory, a, "test_enum_field_register_by_id" + (compatible ? 
"_compatible" : ""));
+  }
+
   @Test
   public void testCrossLanguageMetaShare() throws Exception {
     Fory fory =
diff --git a/python/pyfory/tests/test_cross_language.py 
b/python/pyfory/tests/test_cross_language.py
index 9dd8dd61a..b78337004 100644
--- a/python/pyfory/tests/test_cross_language.py
+++ b/python/pyfory/tests/test_cross_language.py
@@ -465,7 +465,8 @@ def test_serialize_simple_struct(data_file_path):
 
 @cross_language_test
 def test_register_by_id(data_file_path):
-    fory = pyfory.Fory(language=pyfory.Language.XLANG, ref_tracking=True)
+    compatible = "compatible" in data_file_path
+    fory = pyfory.Fory(language=pyfory.XLANG, ref_tracking=True, 
compatible=compatible)
     fory.register_type(ComplexObject2, type_id=100)
     obj = ComplexObject2(f1=True, f2={-1: 2})
     struct_round_back(data_file_path, fory, obj)
@@ -522,6 +523,16 @@ def test_enum_field(data_file_path):
     struct_round_back(data_file_path, fory, obj)
 
 
+@cross_language_test
+def test_enum_field_register_by_id(data_file_path):
+    compatible = "compatible" in data_file_path
+    fory = pyfory.Fory(language=pyfory.Language.XLANG, ref_tracking=False, 
compatible=compatible)
+    fory.register_type(EnumTestClass, type_id=1)
+    fory.register_type(EnumFieldStruct, type_id=2)
+    obj = EnumFieldStruct(f1=EnumTestClass.FOO, f2=EnumTestClass.BAR, f3="abc")
+    struct_round_back(data_file_path, fory, obj)
+
+
 @cross_language_test
 def test_struct_hash(data_file_path):
     with open(data_file_path, "rb") as f:


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to