From 097671a525b0292996f59c73e3174b8d222eeb93 Mon Sep 17 00:00:00 2001 From: mark4z Date: Sun, 3 Jul 2022 15:03:51 +0800 Subject: [PATCH] nil slice decode to empty slice --- list.go | 7 ++++++- list_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/list.go b/list.go index c89bc49d..0ce242b8 100644 --- a/list.go +++ b/list.go @@ -152,7 +152,12 @@ func (e *Encoder) writeTypedList(v interface{}) error { var err error value := reflect.ValueOf(v) - + // https://github.com/apache/dubbo-go-hessian2/issues/317 + // if list is null, just return 'N' + if value.IsNil() { + e.buffer = encByte(e.buffer, BC_NULL) // 'N' + return nil + } // check ref if n, ok := e.checkRefMap(value); ok { e.buffer = encRef(e.buffer, n) diff --git a/list_test.go b/list_test.go index 65783669..849e9bbd 100644 --- a/list_test.go +++ b/list_test.go @@ -207,6 +207,19 @@ func TestListEncode(t *testing.T) { testJavaDecode(t, "customArgTypedFixedList_Object", []Object{new(A0)}) } +func TestNilList(t *testing.T) { + var List []*A0 + e := NewEncoder() + _ = e.Encode(List) + + d := NewDecoder(e.Buffer()) + res, err := d.Decode() + if err != nil { + t.Fail() + } + assert.Nil(t, res) +} + type TypedListTest struct { A *A0 List [][]*A0