Skip to content

Commit

Permalink
fix: #3465 If the value of a string is null, the value of string is n…
Browse files Browse the repository at this point in the history
…il after the string is converted to []string (#3468)
  • Loading branch information
wln32 authored Apr 10, 2024
1 parent b040654 commit d7a0482
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
50 changes: 50 additions & 0 deletions net/ghttp/ghttp_z_unit_feature_router_strict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})
}
17 changes: 16 additions & 1 deletion util/gconv/gconv_slice_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions util/gconv/gconv_z_unit_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d7a0482

Please sign in to comment.