-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Breaking Change on v4.12.0 When Binding to map[string]interface{} -> the field become list #2652
Comments
@slzhffktm I've submitted a pull request (#2656) that addresses this issue by maintaining backwards compatibility for The PR modifies the
However, if the maintainers decide not to merge this change, here's an alternative solution you can use in your code to achieve the same result: func ConvertToSingleValues(input map[string]interface{}) map[string]interface{} {
for key, value := range input {
if strSlice, ok := value.([]string); ok {
if len(strSlice) == 1 {
input[key] = strSlice[0]
}
}
}
return input
}
func ExampleFunction(c echo.Context) error {
request := make(map[string]interface{})
if err := c.Bind(&request); err != nil {
return err
}
request := ConvertToSingleValues(request)
return c.JSON(http.StatusOK, request)
} You can use this helper function with new version without breaking code regardless of whether the PR is merged. It processes the |
Hello @thesaltree ! Seems like your PR is merged, thank you for your contribution!! |
Your return header is not necessary because map types are reference types: func ConvertToSingleValues(input map[string]interface{}) {
for key, value := range input {
if strSlice, ok := value.([]string); ok {
if len(strSlice) == 1 {
input[key] = strSlice[0]
}
}
}
} I stumbled over this issue because I have the same problem after updating to 4.12. |
Issue Description
Breaking change on v4.12.0:
The
c.Bind
is now mapping query params & request body to list of string.Example:
POST /test?query=param
request body
The code
Previous behaviour (pre v4.12.0):
Result:
map[string]interface {}{"query":"param", "field1":"somevalue"}
Current behaviour (v4.12.0):
Result:
map[string]interface {}{"query":[]string{"param"}, "field1":[]string{"somevalue"}}
Checklist
Expected behaviour
The behaviour from the code above should keep on returning non-breaking change and still return this on the newest version:
Result:
map[string]interface {}{"query":"param", "field1":"somevalue"}
Actual behaviour
In v4.12.0, the result from the code above returned different result:
Result:
map[string]interface {}{"query":[]string{"param"}, "field1":[]string{"somevalue"}}
Steps to reproduce
Working code to debug
Version/commit
v4.12.0
The text was updated successfully, but these errors were encountered: