Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(util/gconv): #3465 if the value of a string is null, the value of string is nil after the string is converted to []string #3468

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:/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:/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
Loading