Skip to content

Commit

Permalink
修复在没有querytag时,无法正确解析数组的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteReimu committed Jun 17, 2024
1 parent 41da019 commit a7d8436
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ func withParams(r *resty.Request, in any) error {
for name := range tagMap {
switch name {
case "query":
contentType = "application/x-www-form-urlencoded"
case "json":
contentType = "application/json"
case "form-data":
contentType = "multipart/form-data"
}
}
if contentType == "application/x-www-form-urlencoded" {
_, ok1 := tagMap["json"]
_, ok2 := tagMap["form-data"]
if !ok1 && !ok2 {
// 对query类型的字段进行特殊处理
if fieldType.Type.Kind() == reflect.Slice {
strSlice := make([]string, 0, 4)
Expand All @@ -163,14 +174,7 @@ func withParams(r *resty.Request, in any) error {
}
realVal = strings.Join(strSlice, ",")
}
contentType = "application/x-www-form-urlencoded"
case "json":
contentType = "application/json"
case "form-data":
contentType = "multipart/form-data"
}
}
if contentType == "application/x-www-form-urlencoded" {
r.SetQueryParam(fieldName, cast.ToString(realVal))
} else {
bodyMap[fieldName] = realVal
Expand Down
2 changes: 1 addition & 1 deletion util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func TestFormData(t *testing.T) {

func TestWithParamsSlice(t *testing.T) {
type Test struct {
Ids []int `request:"query"`
Ids []int
IdsA []string `request:"query"`
}

Expand Down

0 comments on commit a7d8436

Please sign in to comment.