Skip to content

Commit

Permalink
Merge pull request #307 from ma642/fix-double-null-value
Browse files Browse the repository at this point in the history
fix #305, Java Double type to float64 get error  when null value set
  • Loading branch information
AlexStocks committed Jan 6, 2022
2 parents 1f4cc01 + c4b8281 commit 41ab0ac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
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

0 comments on commit 41ab0ac

Please sign in to comment.