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 5a8cef01a fix(java): fix field converter for xlang (#2654)
5a8cef01a is described below

commit 5a8cef01a559709463e0bbd6fc0735327b5e45ca
Author: Shawn Yang <[email protected]>
AuthorDate: Wed Sep 24 11:29:25 2025 +0800

    fix(java): fix field converter for xlang (#2654)
    
    <!--
    **Thanks for contributing to Apache Fory™.**
    
    **If this is your first time opening a PR on fory, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fory™** community has requirements on the naming of pr
    titles. You can also find instructions in
    [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
    
    - Apache Fory™ has a strong focus on performance. If the PR you submit
    will have an impact on performance, please benchmark it first and
    provide the benchmark result here.
    -->
    
    ## Why?
    
    <!-- Describe the purpose of this PR. -->
    
    ## What does this PR do?
    
    <!-- Describe the details of this PR. -->
    
    ## Related issues
    
     Fixes #2650
    
    ## 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.
    -->
---
 AGENTS.md                                                |  2 +-
 .../src/main/java/org/apache/fory/meta/ClassDef.java     |  9 ++++++---
 .../src/test/java/org/apache/fory/ForyTestBase.java      |  5 +++++
 .../apache/fory/serializer/MetaSharedCompatibleTest.java | 16 ++++++++++++++--
 4 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/AGENTS.md b/AGENTS.md
index 9db7e7f83..a22fd22cc 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -302,7 +302,7 @@ Fory uses binary protocols for efficient serialization and 
deserialization. Fory
 - **[Java serialization 
format](docs/specification/java_serialization_spec.md)**: Highly-optimized and 
drop-in replacement for Java serialization.
 - **Python serialization format**: Highly-optimized and drop-in replacement 
for Python pickle, which is an extension built upon **[xlang serialization 
format](docs/specification/xlang_serialization_spec.md)**.
 
-**`docs/specification/**` are the specification for the Fory protocol**, 
please read those documents carefully and think hard and make sure you 
understand them before making changes to code and documentation.
+**`docs/specification/**` are the specification for the Fory protocol, please 
read those documents carefully and think hard and make sure you understand them 
before making changes to code and documentation.
 
 ### Core Structure
 
diff --git a/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java 
b/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java
index 7a2bccdbe..a8c0f0294 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/ClassDef.java
@@ -601,10 +601,13 @@ public class ClassDef implements Serializable {
       if (resolver instanceof XtypeResolver) {
         cls = ((XtypeResolver) resolver).getXtypeInfo(classId).getCls();
         if (Types.isPrimitiveType(classId)) {
-          if (declared.isPrimitive() && !nullable) {
+          // For primitive types, ensure we use the correct primitive/boxed 
form
+          // based on the nullable flag, not the declared type
+          if (!nullable) {
+            // nullable=false means the source was primitive, use primitive 
type
             cls = TypeUtils.unwrap(cls);
-          }
-          if (nullable && !declared.isPrimitive()) {
+          } else {
+            // nullable=true means the source was boxed, use boxed type
             cls = TypeUtils.wrap(cls);
           }
         }
diff --git a/java/fory-core/src/test/java/org/apache/fory/ForyTestBase.java 
b/java/fory-core/src/test/java/org/apache/fory/ForyTestBase.java
index f2d595688..7c1814ec3 100644
--- a/java/fory-core/src/test/java/org/apache/fory/ForyTestBase.java
+++ b/java/fory-core/src/test/java/org/apache/fory/ForyTestBase.java
@@ -181,6 +181,11 @@ public abstract class ForyTestBase {
     };
   }
 
+  @DataProvider
+  public static Object[][] language() {
+    return new Object[][] {{Language.JAVA}, {Language.XLANG}};
+  }
+
   @DataProvider(name = "javaFory")
   public static Object[][] javaForyConfig() {
     return new Object[][] {
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/serializer/MetaSharedCompatibleTest.java
 
b/java/fory-core/src/test/java/org/apache/fory/serializer/MetaSharedCompatibleTest.java
index eecb7aa7d..7e9813678 100644
--- 
a/java/fory-core/src/test/java/org/apache/fory/serializer/MetaSharedCompatibleTest.java
+++ 
b/java/fory-core/src/test/java/org/apache/fory/serializer/MetaSharedCompatibleTest.java
@@ -942,8 +942,8 @@ public class MetaSharedCompatibleTest extends ForyTestBase {
     Assert.assertEquals(ReflectionUtils.getObjectFieldValue(o1, "f3"), 30);
   }
 
-  @Test
-  public void testCompatibleFieldConvert() throws Exception {
+  @Test(dataProvider = "language")
+  public void testCompatibleFieldConvert(Language language) throws Exception {
     byte[] bytes;
     Object o1;
     ImmutableSet<String> floatFields = ImmutableSet.of("f11", "f12", "f13", 
"f14");
@@ -985,9 +985,13 @@ public class MetaSharedCompatibleTest extends ForyTestBase 
{
       }
       Fory fory =
           builder()
+              .withLanguage(language)
               .withCompatibleMode(CompatibleMode.COMPATIBLE)
               .withClassLoader(classLoader)
               .build();
+      if (language == Language.XLANG) {
+        fory.register(cls);
+      }
       bytes = fory.serialize(o1);
     }
     {
@@ -1019,9 +1023,13 @@ public class MetaSharedCompatibleTest extends 
ForyTestBase {
       Assert.assertNotEquals(cls, o1.getClass());
       Fory fory =
           builder()
+              .withLanguage(language)
               .withCompatibleMode(CompatibleMode.COMPATIBLE)
               .withClassLoader(classLoader)
               .build();
+      if (language == Language.XLANG) {
+        fory.register(cls);
+      }
       Object o = fory.deserialize(bytes);
       Assert.assertEquals(o.getClass(), cls);
       List<Field> fields = ReflectionUtils.getSortedFields(cls, false);
@@ -1064,11 +1072,15 @@ public class MetaSharedCompatibleTest extends 
ForyTestBase {
           JaninoUtils.compile(Thread.currentThread().getContextClassLoader(), 
compileUnit);
       Fory fory =
           builder()
+              .withLanguage(language)
               .withCompatibleMode(CompatibleMode.COMPATIBLE)
               .withClassLoader(classLoader)
               .build();
       Class<?> cls = 
classLoader.loadClass(compileUnit.getQualifiedClassName());
       Assert.assertNotEquals(cls, o1.getClass());
+      if (language == Language.XLANG) {
+        fory.register(cls);
+      }
       Object o = fory.deserialize(bytes);
       Assert.assertEquals(o.getClass(), cls);
       List<Field> fields = ReflectionUtils.getSortedFields(cls, false);


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

Reply via email to