Skip to content

fix(gstr): preserve repeated query parameters in Parse (fix #4751) - #4817

Open
waterWang wants to merge 1 commit into
gogf:masterfrom
waterWang:fix/gstr-parse-repeated-key-overwrite
Open

fix(gstr): preserve repeated query parameters in Parse (fix #4751)#4817
waterWang wants to merge 1 commit into
gogf:masterfrom
waterWang:fix/gstr-parse-repeated-key-overwrite

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

fix(gstr): preserve repeated query parameters in Parse (fix #4751)

What

Fix gstr.Parse to preserve repeated query parameters when a plain key appears multiple times. Previously, ?name=a&name=b would only keep the last value ("b"), now it correctly produces ["a", "b"].

Why

When a GoFrame request handler receives a URL like ?names=a&names=b and the target struct field is []string, r.Parse(&req) should bind both values. The gstr.Parse function's build method was overwriting the existing value instead of collecting all values into a slice.

How

Changed the build function's length == 1 branch to check if the key already exists in the result map. If it does, both the existing and new values are converted to a []any slice and appended. This is consistent with the existing [] notation behavior (v[]=a&v[]=b[a b]), but without requiring explicit slice notation.

Documentation updates

The Parse function's doc comment is updated to reflect the new behavior:

  • v=m&v=n -> map[v:[m n]] (was map[v:n])

Testing

Before:

?names=a&names=b → Parse returns {"names": "b"} → bind to []string → ["b"]

After:

?names=a&names=b → Parse returns {"names": ["a", "b"]} → bind to []string → ["a", "b"]

Fixes #4751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Request.Parse only keeps the last repeated query value for []string

1 participant