Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support java integer null #296

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
19 changes: 19 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
}
20 changes: 20 additions & 0 deletions test_hessian/src/main/java/test/TestCustomReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Integer> 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);
Expand Down
18 changes: 18 additions & 0 deletions test_hessian/src/main/java/test/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> list;

public User() {
}

Expand All @@ -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<Integer> getList() { return list; }

public void setList(List<Integer> list) { this.list = list; }

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
"id='" + id + '\'' +
'}';
}
}