upangka opened a new issue, #80:
URL: https://github.com/apache/dubbo-hessian-lite/issues/80

   ## Environment
   1. Hessian Version: com.alibaba.com.caucho.hessian (latest)4.0.3
   2. Java Version: JDK 17+ (Record feature required)
   3. OS: Any
   
   `pom.xml`
   ```xml
   <dependencies>
           <dependency>
               <groupId>org.apache.dubbo</groupId>
               <artifactId>hessian-lite</artifactId>
               <version>4.0.3</version>
           </dependency>
           <dependency>
               <groupId>org.projectlombok</groupId>
               <artifactId>lombok</artifactId>
               <version>1.18.36</version>
           </dependency>
       </dependencies>
   ```
   
   ## Problem Description
   When serializing and deserializing a record object graph containing shared 
references (e.g., two Student records sharing the same Address), Hessian2 
throws an IndexOutOfBoundsException. The same scenario works correctly with 
Lombok @Data classes.
   
   ## Steps to Reproduce
   ```java
       record Address(String city) implements Serializable {}
       record Student(String id,String name,Address address) implements 
Serializable{}
       record StuRep(List<Student> students) implements Serializable{}
   
       public static void main(String[] args) throws Exception {
           Address address1 = new Address("BeiJing");
           Address address2 = new Address("ShangHai");
   
           Student student1 = new Student("1001", "ZhangSan", address1);
           // issue here Shared address1
           Student student2 = new Student("1002", "Lisi", address1);
   
           StuRep stuRep = new StuRep(List.of(student1, student2));
   
           // serialize
           byte[] serializedData = serialize(stuRep);
   
           // deserialize
           StuRep deserializedStuRep = deserialize(serializedData);
       }
   
       public static byte[] serialize(StuRep obj) throws Exception {
           ByteArrayOutputStream bos = new ByteArrayOutputStream();
           Hessian2Output out = new Hessian2Output(bos);
   
           out.writeObject(obj);
           out.close();
   
           return bos.toByteArray();
       }
   
       public static StuRep deserialize(byte[] data) throws Exception {
           ByteArrayInputStream bis = new ByteArrayInputStream(data);
           Hessian2Input in = new Hessian2Input(bis);
   
           StuRep obj = (StuRep) in.readObject();
           in.close();
   
           return obj;
       }
   ```
   
   **Error**
   
   ```sh
   Caused by: java.lang.IndexOutOfBoundsException: Index 3 out of bounds for 
length 3
       at com.alibaba.com.caucho.hessian.io.RecordDeserializer.readObject
   ```
   
   ## Expected Behavior
   Deserialization should succeed, preserving object reference sharing (same 
behavior as Lombok @Data classes).
   
   ```java
   @Data
       @AllArgsConstructor
       @NoArgsConstructor
       static class Address implements Serializable {
           private String city;
       }
   
   
       @Data
       @AllArgsConstructor
       @NoArgsConstructor
       static class Student implements Serializable{
           private String id;
           private String name;
           private Address address;
       }
   
       @Data
       @AllArgsConstructor
       @NoArgsConstructor
       static class StuRep implements Serializable{
           List<Student> students;
       }
   
   
       public static void main(String[] args) throws Exception {
           Address address1 = new Address("BeiJing");
           Address address2 = new Address("ShangHai");
   
           Student student1 = new Student("1001", "zhangSan", address1);
           Student student2 = new Student("1002", "LiSi", address1);
   
           StuRep stuRep = new StuRep(List.of(student1, student2));
   
           // serialize
           byte[] serializedData = serialize(stuRep);
   
           // deserialize
           StuRep deserializedStuRep = deserialize(serializedData);
       }
   
   
       public static byte[] serialize(StuRep obj) throws Exception {
           ByteArrayOutputStream bos = new ByteArrayOutputStream();
           Hessian2Output out = new Hessian2Output(bos);
   
           out.writeObject(obj);
           out.close();
   
           return bos.toByteArray();
       }
   
       public static StuRep deserialize(byte[] data) throws Exception {
           ByteArrayInputStream bis = new ByteArrayInputStream(data);
           Hessian2Input in = new Hessian2Input(bis);
   
           StuRep obj = (StuRep) in.readObject();
           in.close();
   
           return obj;
       }
   ```
   
   # Actual Behavior
   ✅ Works: Lombok classes with @Data (shared references handled correctly).
   
   ❌ Fails: Java Records with shared references (throws 
IndexOutOfBoundsException).


-- 
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]

Reply via email to