Repository: zeromicro/go-zero
Pull request #1951 (2022-06-01 10:03:34)

fix: panic on convert to string on fillSliceFromString()
 fix: 修复fillSliceFromString()方法中mapValue 强转string后的panic 错误


Patch:
diff --git a/ROADMAP.md b/ROADMAP.md
index 517c6126..83f9723a 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -22,7 +22,7 @@ We hope that the items listed below will inspire further engagement from the com
 ## 2022
 - [x] Support `context` in redis related methods for timeout and tracing
 - [x] Support `context` in sql related methods for timeout and tracing
-- [ ] Support `context` in mongodb related methods for timeout and tracing
+- [x] Support `context` in mongodb related methods for timeout and tracing
 - [x] Add `httpc.Do` with HTTP call governance, like circuit breaker etc.
 - [ ] Support `goctl doctor` command to report potential issues for given service
 - [ ] Support `goctl mock` command to start a mocking server with given `.api` file
diff --git a/core/bloom/bloom.go b/core/bloom/bloom.go
index a3ab8a29..bc08df25 100644
--- a/core/bloom/bloom.go
+++ b/core/bloom/bloom.go
@@ -69,6 +69,7 @@ func (f *Filter) Exists(data []byte) (bool, error) {
 	if err != nil {
 		return false, err
 	}
+
 	return isSet, nil
 }
 
diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go
index b86ef8f4..56e58839 100644
--- a/core/mapping/unmarshaler.go
+++ b/core/mapping/unmarshaler.go
@@ -498,8 +498,15 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
 
 func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.Value, mapValue interface{}) error {
 	var slice []interface{}
-	if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
-		return err
+	switch v := mapValue.(type) {
+	case json.Number:
+		if err := jsonx.UnmarshalFromString(v.String(), &slice); err != nil {
+			return err
+		}
+	default:
+		if err := jsonx.UnmarshalFromString(mapValue.(string), &slice); err != nil {
+			return err
+		}
 	}
 
 	baseFieldType := Deref(fieldType.Elem())
diff --git a/zrpc/internal/serverinterceptors/statinterceptor.go b/zrpc/internal/serverinterceptors/statinterceptor.go
index a921897e..16302c14 100644
--- a/zrpc/internal/serverinterceptors/statinterceptor.go
+++ b/zrpc/internal/serverinterceptors/statinterceptor.go
@@ -26,10 +26,6 @@ func SetSlowThreshold(threshold time.Duration) {
 func UnaryStatInterceptor(metrics *stat.Metrics) grpc.UnaryServerInterceptor {
 	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
 		handler grpc.UnaryHandler) (resp interface{}, err error) {
-		defer handleCrash(func(r interface{}) {
-			err = toPanicError(r)
-		})
-
 		startTime := timex.Now()
 		defer func() {
 			duration := timex.Since(startTime)


Test patch:
diff --git a/core/logx/rotatelogger_test.go b/core/logx/rotatelogger_test.go
index a7486d95..358fe716 100644
--- a/core/logx/rotatelogger_test.go
+++ b/core/logx/rotatelogger_test.go
@@ -98,6 +98,10 @@ func TestRotateLoggerRotate(t *testing.T) {
 	case *os.LinkError:
 		// avoid rename error on docker container
 		assert.Equal(t, syscall.EXDEV, v.Err)
+	case *os.PathError:
+		// ignore remove error for tests,
+		// files are cleaned in GitHub actions.
+		assert.Equal(t, "remove", v.Op)
 	default:
 		assert.Nil(t, err)
 	}
diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go
index 0bf1739c..f3b037f0 100644
--- a/core/mapping/unmarshaler_test.go
+++ b/core/mapping/unmarshaler_test.go
@@ -2789,3 +2789,14 @@ func BenchmarkDefaultValue(b *testing.B) {
 		}
 	}
 }
+
+func TestUnmarshalJsonReaderArray(t *testing.T) {
+	payload := "{\"id\": 123}"
+	var res struct {
+		ID []string   `json:"id"`
+	}
+	reader := strings.NewReader(payload)
+	err := UnmarshalJsonReader(reader, &res)
+	assert.Nil(t, err)
+	assert.Equal(t, 1, len(res.ID))
+}
diff --git a/zrpc/internal/serverinterceptors/statinterceptor_test.go b/zrpc/internal/serverinterceptors/statinterceptor_test.go
index e3e3f424..b9a4706c 100644
--- a/zrpc/internal/serverinterceptors/statinterceptor_test.go
+++ b/zrpc/internal/serverinterceptors/statinterceptor_test.go
@@ -30,17 +30,6 @@ func TestUnaryStatInterceptor(t *testing.T) {
 	assert.Nil(t, err)
 }
 
-func TestUnaryStatInterceptor_crash(t *testing.T) {
-	metrics := stat.NewMetrics("mock")
-	interceptor := UnaryStatInterceptor(metrics)
-	_, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{
-		FullMethod: "/",
-	}, func(ctx context.Context, req interface{}) (interface{}, error) {
-		panic("error")
-	})
-	assert.NotNil(t, err)
-}
-
 func TestLogDuration(t *testing.T) {
 	addrs, err := net.InterfaceAddrs()
 	assert.Nil(t, err)