Mantis - Hessian
Viewing Issue Advanced Details
5553 minor always 10-11-13 03:30 08-14-23 08:01
mbutov  
 
normal  
new 4.0.28  
open  
none    
none  
0005553: Deserialization fails if there are fields of the same name in the class and its superclass.
TEST

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;

import org.junit.Test;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

public class HessianTest {

    static class A implements Serializable {

        private String x = "x";

        @Override
        public String toString() {
            return "A.x=" + x;
        }
    }

    static class B extends A {

        private int x = 1;

        @Override
        public String toString() {
            return super.toString() + ",B.x='" + x;
        }
    }

    @Test
    public void testSameFieldName() throws Throwable {

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        B b1 = new B();
        System.out.println(b1);

        Hessian2Output output = new Hessian2Output(buffer);
        output.writeObject(b1);
        output.flush();

        Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(buffer.toByteArray()));
        B b2 = (B) input.readObject();
        System.out.println(b2);

    }


}

OUTPUT

com.caucho.hessian.io.HessianFieldException: org.proxyremoting.HessianTest$B.x: expected integer at 0x1 java.lang.String (x)
  [C\x1forg.proxyremoting.HessianTest$B\x92\x01x\x01x`\x91] \x01 [x]
    at com.caucho.hessian.io.UnsafeDeserializer.logDeserializeError(UnsafeDeserializer.java:786)
    at com.caucho.hessian.io.UnsafeDeserializer$IntFieldDeserializer.deserialize(UnsafeDeserializer.java:565)
    at com.caucho.hessian.io.UnsafeDeserializer.readObject(UnsafeDeserializer.java:239)
    at com.caucho.hessian.io.UnsafeDeserializer.readObject(UnsafeDeserializer.java:150)
    at com.caucho.hessian.io.Hessian2Input.readObjectInstance(Hessian2Input.java:2219)
    at com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2140)
    at com.caucho.hessian.io.Hessian2Input.readObject(Hessian2Input.java:2124)
    at org.proxyremoting.HessianTest.testSameFieldName(HessianTest.java:50)
Caused by: com.caucho.hessian.io.HessianProtocolException: expected integer at 0x1 java.lang.String (x)
  [C\x1forg.proxyremoting.HessianTest$B\x92\x01x\x01x`\x91] \x01 [x]
    at com.caucho.hessian.io.Hessian2Input.error(Hessian2Input.java:2943)
    at com.caucho.hessian.io.Hessian2Input.expect(Hessian2Input.java:2880)
    at com.caucho.hessian.io.Hessian2Input.readInt(Hessian2Input.java:825)
    at com.caucho.hessian.io.UnsafeDeserializer$IntFieldDeserializer.deserialize(UnsafeDeserializer.java:561)
    ... 32 more

There are no notes attached to this issue.