From 85ab8ea0234dc3a6e6fd11092da3ee72f25365a6 Mon Sep 17 00:00:00 2001 From: wln <732437837@qq.com> Date: Wed, 10 Apr 2024 09:10:41 +0800 Subject: [PATCH] fix: #3465 If the value of a string is null, the value of string is nil after the string is converted to []string --- ...ghttp_z_unit_feature_router_strict_test.go | 50 +++++++++++++++++++ util/gconv/gconv_slice_str.go | 17 ++++++- util/gconv/gconv_z_unit_slice_test.go | 6 +++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/net/ghttp/ghttp_z_unit_feature_router_strict_test.go b/net/ghttp/ghttp_z_unit_feature_router_strict_test.go index 923c3ab2cb4..fba43a89488 100644 --- a/net/ghttp/ghttp_z_unit_feature_router_strict_test.go +++ b/net/ghttp/ghttp_z_unit_feature_router_strict_test.go @@ -485,3 +485,53 @@ func Test_JsonRawMessage_Issue3449(t *testing.T) { }) } + +type testNullStringIssue3465Req struct { + g.Meta `path:"/test" method:"get" sm:"hello" tags:"示例"` + Name []string `json:"name" v:"required"` +} +type testNullStringIssue3465Res struct { + Name []string `json:"name" v:"required" ` +} + +type testNullStringIssue3465 struct { +} + +func (t *testNullStringIssue3465) Test(ctx context.Context, req *testNullStringIssue3465Req) (res *testNullStringIssue3465Res, err error) { + return &testNullStringIssue3465Res{ + Name: req.Name, + }, nil +} + +// https://github.com/gogf/gf/issues/3465 +func Test_NullString_Issue3465(t *testing.T) { + + s := g.Server(guid.S()) + s.Use(ghttp.MiddlewareHandlerResponse) + s.Group("/", func(group *ghttp.RouterGroup) { + group.Bind(new(testNullStringIssue3465)) + }) + s.SetDumpRouterMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.C(t, func(t *gtest.T) { + client := g.Client() + client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) + + data1 := map[string]any{ + "name": "null", + } + + expect1 := `{"code":0,"message":"","data":{"name":["null"]}}` + t.Assert(client.GetContent(ctx, "/test", data1), expect1) + + data2 := map[string]any{ + "name": []string{"null", "null"}, + } + expect2 := `{"code":0,"message":"","data":{"name":["null","null"]}}` + t.Assert(client.GetContent(ctx, "/test", data2), expect2) + + }) +} diff --git a/util/gconv/gconv_slice_str.go b/util/gconv/gconv_slice_str.go index c085d271fbc..8c97c0157d3 100644 --- a/util/gconv/gconv_slice_str.go +++ b/util/gconv/gconv_slice_str.go @@ -60,11 +60,26 @@ func Strings(any interface{}) []string { case []uint8: if json.Valid(value) { _ = json.UnmarshalUseNumber(value, &array) - } else { + } + if array == nil { array = make([]string, len(value)) for k, v := range value { array[k] = String(v) } + return array + } + case string: + byteValue := []byte(value) + if json.Valid(byteValue) { + _ = json.UnmarshalUseNumber(byteValue, &array) + } + if array == nil { + if value == "" { + return []string{} + } + // Prevent strings from being null + // See Issue 3465 for details + return []string{value} } case []uint16: array = make([]string, len(value)) diff --git a/util/gconv/gconv_z_unit_slice_test.go b/util/gconv/gconv_z_unit_slice_test.go index d17d017599b..4103ec18396 100644 --- a/util/gconv/gconv_z_unit_slice_test.go +++ b/util/gconv/gconv_z_unit_slice_test.go @@ -321,6 +321,12 @@ func Test_Strings(t *testing.T) { gtest.C(t, func(t *gtest.T) { t.AssertEQ(gconv.Strings("123"), []string{"123"}) }) + // https://github.com/gogf/gf/issues/3465 + gtest.C(t, func(t *gtest.T) { + t.AssertEQ(gconv.Strings("null"), []string{"null"}) + t.AssertEQ(gconv.Strings([]byte("null")), []string{"110", "117", "108", "108"}) + t.AssertEQ(gconv.Strings("{\"name\":\"wln\"}"), []string{"{\"name\":\"wln\"}"}) + }) } func Test_Slice_Interfaces(t *testing.T) {