From df620103579cea31934dad311d3dd60ea810af5f Mon Sep 17 00:00:00 2001 From: sanxun0325 Date: Fri, 3 Dec 2021 23:20:19 +0800 Subject: [PATCH] support java integer null --- codec.go | 6 +++++- int.go | 3 +++ int_test.go | 1 + object_test.go | 19 ++++++++++++++++++ .../src/main/java/test/TestCustomReply.java | 20 +++++++++++++++++++ .../src/main/java/test/model/User.java | 18 +++++++++++++++++ 6 files changed, 66 insertions(+), 1 deletion(-) diff --git a/codec.go b/codec.go index 425468c9..2055a12c 100644 --- a/codec.go +++ b/codec.go @@ -455,7 +455,11 @@ func ConvertSliceValueType(destTyp reflect.Type, v reflect.Value) (reflect.Value if cv, ok := item.(reflect.Value); ok { itemValue = cv } else { - itemValue = reflect.ValueOf(item) + if item == nil { + itemValue = reflect.Zero(destTyp.Elem()) + } else { + itemValue = reflect.ValueOf(item) + } } if !elemPtrType && itemValue.Kind() == reflect.Ptr { diff --git a/int.go b/int.go index dd06d11a..06cdb719 100644 --- a/int.go +++ b/int.go @@ -102,6 +102,9 @@ func (d *Decoder) decInt32(flag int32) (int32, error) { err = binary.Read(d.reader, binary.BigEndian, &i32) return i32, perrors.WithStack(err) + case tag == BC_NULL: + return int32(0), nil + default: return 0, perrors.Errorf("decInt32 integer wrong tag:%#x", tag) } diff --git a/int_test.go b/int_test.go index 0c98ff6f..441bca36 100644 --- a/int_test.go +++ b/int_test.go @@ -116,6 +116,7 @@ func TestEncInt32Len4B(t *testing.T) { } func TestInt(t *testing.T) { + testDecodeFramework(t, "replyNull", nil) testDecodeFramework(t, "replyInt_0", int32(0)) testDecodeFramework(t, "replyInt_0x30", int32(0x30)) testDecodeFramework(t, "replyInt_0x3ffff", int32(0x3ffff)) diff --git a/object_test.go b/object_test.go index f2d1e4f3..d6d67b19 100644 --- a/object_test.go +++ b/object_test.go @@ -897,3 +897,22 @@ func TestWrapperClassArray(t *testing.T) { da := &DoubleArray{Values: []float64{1.0, 100.0, 10000.1}} assert.True(t, reflect.DeepEqual(got, da)) } + +type User struct { + Id int32 + List []int32 +} + +func (u *User) JavaClassName() string { + return "test.model.User" +} + +func TestDecodeIntegerHasNull(t *testing.T) { + RegisterPOJO(&User{}) + testDecodeFramework(t, "customReplyTypedIntegerHasNull", &User{Id: 0}) +} + +func TestDecodeSliceIntegerHasNull(t *testing.T) { + RegisterPOJO(&User{}) + testDecodeFramework(t, "customReplyTypedListIntegerHasNull", &User{Id: 0, List: []int32{1, 0}}) +} diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java index 3ae19de9..8d22362e 100644 --- a/test_hessian/src/main/java/test/TestCustomReply.java +++ b/test_hessian/src/main/java/test/TestCustomReply.java @@ -22,10 +22,12 @@ import com.alibaba.fastjson.JSONObject; import com.caucho.hessian.test.A0; import com.caucho.hessian.test.A1; +import com.caucho.hessian.test.TestObject; import test.generic.BusinessData; import test.generic.Response; import test.model.CustomMap; import test.model.DateDemo; +import test.model.User; import java.io.OutputStream; import java.io.Serializable; @@ -92,6 +94,24 @@ public void customReplyJsonString() throws Exception { output.flush(); } + public void customReplyTypedIntegerHasNull() throws Exception { + User user = new User(); + user.setId(null); + output.writeObject(user); + output.flush(); + } + + public void customReplyTypedListIntegerHasNull() throws Exception { + User user = new User(); + user.setId(null); + List list = new ArrayList<>(); + list.add(1); + list.add(null); + user.setList(list); + output.writeObject(user); + output.flush(); + } + public void customReplyTypedFixedListHasNull() throws Exception { Object[] o = new Object[]{new A0(), new A1(), null}; output.writeObject(o); diff --git a/test_hessian/src/main/java/test/model/User.java b/test_hessian/src/main/java/test/model/User.java index a9f5e523..2a0538a3 100644 --- a/test_hessian/src/main/java/test/model/User.java +++ b/test_hessian/src/main/java/test/model/User.java @@ -18,10 +18,15 @@ package test.model; import java.io.Serializable; +import java.util.List; public class User implements Serializable { private String name; + private Integer id; + + private List list; + public User() { } @@ -37,10 +42,23 @@ public void setName(String name) { this.name = name; } + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public List getList() { return list; } + + public void setList(List list) { this.list = list; } + @Override public String toString() { return "User{" + "name='" + name + '\'' + + "id='" + id + '\'' + '}'; } }