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 cannot process array of values in application/x-www-form-urlencoded request #1873

Merged
merged 3 commits into from Apr 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions ctx.go
Expand Up @@ -341,10 +341,20 @@ func (c *Ctx) BodyParser(out interface{}) error {
}
if strings.HasPrefix(ctype, MIMEApplicationForm) {
data := make(map[string][]string)
var err error

c.fasthttp.PostArgs().VisitAll(func(key, val []byte) {
if err != nil {
return
}

k := utils.UnsafeString(key)
v := utils.UnsafeString(val)

if strings.Contains(k, "[") {
k, err = parseParamSquareBrackets(k)
}

if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) {
values := strings.Split(v, ",")
for i := 0; i < len(values); i++ {
Expand Down Expand Up @@ -929,7 +939,7 @@ func (c *Ctx) QueryParser(out interface{}) error {
v := utils.UnsafeString(val)

if strings.Contains(k, "[") {
k, err = parseQuery(k)
k, err = parseParamSquareBrackets(k)
}

if strings.Contains(v, ",") && equalFieldType(out, reflect.Slice, k) {
Expand All @@ -950,7 +960,7 @@ func (c *Ctx) QueryParser(out interface{}) error {
return c.parseToStruct(queryTag, out, data)
}

func parseQuery(k string) (string, error) {
func parseParamSquareBrackets(k string) (string, error) {
bb := bytebufferpool.Get()
defer bytebufferpool.Put(bb)

Expand Down
24 changes: 24 additions & 0 deletions ctx_test.go
Expand Up @@ -399,6 +399,30 @@ func Test_Ctx_BodyParser(t *testing.T) {

testDecodeParserError("invalid-content-type", "")
testDecodeParserError(MIMEMultipartForm+`;boundary="b"`, "--b")

type CollectionQuery struct {
Data []Demo `query:"data"`
}

c.Request().Reset()
c.Request().Header.SetContentType(MIMEApplicationForm)
c.Request().SetBody([]byte("data[0][name]=john&data[1][name]=doe"))
c.Request().Header.SetContentLength(len(c.Body()))
cq := new(CollectionQuery)
utils.AssertEqual(t, nil, c.BodyParser(cq))
utils.AssertEqual(t, 2, len(cq.Data))
utils.AssertEqual(t, "john", cq.Data[0].Name)
utils.AssertEqual(t, "doe", cq.Data[1].Name)

c.Request().Reset()
c.Request().Header.SetContentType(MIMEApplicationForm)
c.Request().SetBody([]byte("data.0.name=john&data.1.name=doe"))
c.Request().Header.SetContentLength(len(c.Body()))
cq = new(CollectionQuery)
utils.AssertEqual(t, nil, c.BodyParser(cq))
utils.AssertEqual(t, 2, len(cq.Data))
utils.AssertEqual(t, "john", cq.Data[0].Name)
utils.AssertEqual(t, "doe", cq.Data[1].Name)
}

// go test -run Test_Ctx_BodyParser_WithSetParserDecoder
Expand Down