You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by wo...@apache.org on 2021/07/05 02:00:07 UTC

[dubbo-go-hessian2] branch master updated: fix getting nil result for decoding list to generic interface field (#269)

This is an automated email from the ASF dual-hosted git repository.

wongoo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-go-hessian2.git


The following commit(s) were added to refs/heads/master by this push:
     new e714430  fix getting nil result for decoding list to generic interface field (#269)
e714430 is described below

commit e7144303cf1a338c905e31a574e57bf3effee6a5
Author: 望哥 <ge...@163.com>
AuthorDate: Mon Jul 5 09:59:57 2021 +0800

    fix getting nil result for decoding list to generic interface field (#269)
---
 codec.go                                           | 23 ++++++++------
 object.go                                          |  8 +++--
 object_test.go                                     | 35 ++++++++++++++++++++++
 test_hessian/pom.xml                               |  8 ++---
 .../src/main/java/test/TestCustomReply.java        | 20 ++++++++++++-
 5 files changed, 78 insertions(+), 16 deletions(-)

diff --git a/codec.go b/codec.go
index 2df9e58..5087efa 100644
--- a/codec.go
+++ b/codec.go
@@ -397,14 +397,7 @@ func SetSlice(dest reflect.Value, objects interface{}) error {
 	}
 
 	if ref, ok := objects.(*_refHolder); ok {
-		v, err := ConvertSliceValueType(destTyp, ref.value)
-		if err != nil {
-			return err
-		}
-		SetValue(dest, v)
-		ref.change(v) // change finally
-		ref.notify()  // delay set value to all destinations
-		return nil
+		return unpackRefHolder(dest, destTyp, ref)
 	}
 
 	v := EnsurePackValue(objects)
@@ -422,9 +415,21 @@ func SetSlice(dest reflect.Value, objects interface{}) error {
 	return nil
 }
 
+// unpackRefHolder unpack the ref holder when decoding slice finished.
+func unpackRefHolder(dest reflect.Value, destTyp reflect.Type, ref *_refHolder) error {
+	v, err := ConvertSliceValueType(destTyp, ref.value)
+	if err != nil {
+		return err
+	}
+	SetValue(dest, v)
+	ref.change(v) // change finally
+	ref.notify()  // delay set value to all destinations
+	return nil
+}
+
 // ConvertSliceValueType convert to slice of destination type
 func ConvertSliceValueType(destTyp reflect.Type, v reflect.Value) (reflect.Value, error) {
-	if destTyp == v.Type() {
+	if destTyp == v.Type() || destTyp.Kind() == reflect.Interface {
 		return v, nil
 	}
 
diff --git a/object.go b/object.go
index 0172162..0186992 100644
--- a/object.go
+++ b/object.go
@@ -539,8 +539,12 @@ func (d *Decoder) decInstance(typ reflect.Type, cls *classInfo) (interface{}, er
 				return nil, perrors.WithStack(err)
 			}
 			if s != nil {
-				// set value which accepting pointers
-				SetValue(fldRawValue, EnsurePackValue(s))
+				if ref, ok := s.(*_refHolder); ok {
+					_ = unpackRefHolder(fldRawValue, fldTyp, ref)
+				} else {
+					// set value which accepting pointers
+					SetValue(fldRawValue, EnsurePackValue(s))
+				}
 			}
 		default:
 			return nil, perrors.Errorf("unknown struct member type: %v %v", kind, typ.Name()+"."+fieldStruct.Name)
diff --git a/object_test.go b/object_test.go
index ebc5b5d..e5b51f1 100644
--- a/object_test.go
+++ b/object_test.go
@@ -812,3 +812,38 @@ func TestCustomReplyGenericResponseBusinessData(t *testing.T) {
 
 	testDecodeFramework(t, "customReplyGenericResponseBusinessData", res)
 }
+
+func TestCustomReplyGenericResponseList(t *testing.T) {
+	data := []*BusinessData{
+		{
+			Name:  "apple",
+			Count: 5,
+		},
+		{
+			Name:  "banana",
+			Count: 6,
+		},
+	}
+	res := &GenericResponse{
+		Code: 202,
+		Data: data,
+	}
+	RegisterPOJO(data[0])
+	RegisterPOJO(res)
+
+	testDecodeFrameworkFunc(t, "customReplyGenericResponseList", func(r interface{}) {
+		expect, ok := r.(*GenericResponse)
+		if !ok {
+			t.Errorf("expect *GenericResponse, but get %v", r)
+			return
+		}
+		list, dataOk := expect.Data.([]interface{})
+		if !dataOk {
+			t.Errorf("expect []interface{}, but get %v", expect.Data)
+			return
+		}
+		assert.Equal(t, res.Code, expect.Code)
+		assert.True(t, reflect.DeepEqual(data[0], list[0]))
+		assert.True(t, reflect.DeepEqual(data[1], list[1]))
+	})
+}
diff --git a/test_hessian/pom.xml b/test_hessian/pom.xml
index 436a8d0..e6e185d 100644
--- a/test_hessian/pom.xml
+++ b/test_hessian/pom.xml
@@ -27,13 +27,13 @@
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>hessian-lite</artifactId>
-            <version>3.2.6</version>
+            <version>3.2.7</version>
             <scope>compile</scope>
         </dependency>
         <dependency>
             <groupId>com.caucho</groupId>
             <artifactId>hessian</artifactId>
-            <version>4.0.60</version>
+            <version>4.0.65</version>
             <scope>compile</scope>
         </dependency>
         <dependency>
@@ -50,12 +50,12 @@
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
-            <version>1.2.70</version>
+            <version>1.2.76</version>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>4.13</version>
+            <version>4.13.2</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git a/test_hessian/src/main/java/test/TestCustomReply.java b/test_hessian/src/main/java/test/TestCustomReply.java
index 08eacaa..f367be1 100644
--- a/test_hessian/src/main/java/test/TestCustomReply.java
+++ b/test_hessian/src/main/java/test/TestCustomReply.java
@@ -31,7 +31,16 @@ import java.io.OutputStream;
 import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
 
 
 public class TestCustomReply {
@@ -599,6 +608,15 @@ public class TestCustomReply {
         output.flush();
     }
 
+    public void customReplyGenericResponseList() throws Exception {
+        List<BusinessData> list = new ArrayList<>();
+        list.add(new BusinessData("apple", 5));
+        list.add(new BusinessData("banana", 6));
+        Response<List<BusinessData>> response = new Response<>(202, list);
+        output.writeObject(response);
+        output.flush();
+    }
+
     public void customReplyUUID() throws Exception {
         Map<String, Object> map = new HashMap<String, Object>();
         UUID uuid1 = new UUID(459021424248441700L, -7160773830801198154L);