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

fix #305, Java Double type to float64 get error when null value set #306

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions double.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func (d *Decoder) decDouble(flag int32) (interface{}, error) {
var f64 float64
err = binary.Read(d.reader, binary.BigEndian, &f64)
return f64, perrors.WithStack(err)

case BC_NULL:
return float64(0), nil
}

return nil, perrors.Errorf("decDouble parse double wrong tag:%d-%#x", int(tag), tag)
Expand Down
43 changes: 43 additions & 0 deletions double_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package hessian

import (
"reflect"
"testing"
)

Expand Down Expand Up @@ -69,7 +70,35 @@ func TestIssue181(t *testing.T) {
t.Logf("decode(%v) = %v\n", v, res)
}

func TestDoubleNull(t *testing.T) {
var (
v float32
err error
d *Decoder
res interface{}
)

// Double type with null value
d = NewDecoder([]byte{'C', 'N'})
res, err = d.Decode()
if err != nil {
t.Errorf("get error when decode %v", err)
return
}
f, ok := res.(float64)
if !ok {
t.Errorf("float64 is expected but %v", reflect.TypeOf(res))
return
}
if float32(f) != 0.0 {
t.Errorf("expect float32")
return
}
t.Logf("decode(%v) = %v\n", v, res)
}

func TestDouble(t *testing.T) {
testDecodeFramework(t, "replyNull", nil)
testDecodeFramework(t, "replyDouble_0_0", 0.0)
testDecodeFramework(t, "replyDouble_0_001", 0.001)
testDecodeFramework(t, "replyDouble_1_0", 1.0)
Expand Down Expand Up @@ -109,3 +138,17 @@ func TestDoublePrtEncode(t *testing.T) {
testSimpleEncode(t, &f0)
testSimpleEncode(t, &f1)
}

type score struct {
Score float64
Name string
}

func (s score) JavaClassName() string {
return "test.Score"
}

func TestDecodeDoubleHasNull(t *testing.T) {
RegisterPOJO(&score{})
testDecodeFramework(t, "customReplyTypedDoubleHasNull", &score{Score: 0, Name: "DoubleWithNull"})
}
29 changes: 29 additions & 0 deletions test_hessian/src/main/java/test/TestCustomReply.java
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,35 @@ public void customReplyEnumSet() throws Exception {
output.writeObject(map);
output.flush();
}

public void customReplyTypedDoubleHasNull() throws Exception {

Score s = new Score();
s.setScore(null);
s.setName("DoubleWithNull");
output.writeObject(s);
output.flush();
}
}

class Score implements Serializable {
private Double score;
private String name;

public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}

public void setScore(Double score) {
this.score = score;
}

public Double getScore() {
return this.score;
}
}

interface Leg {
Expand Down