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

Breaking Change on v4.12.0 When Binding to map[string]interface{} -> the field become list #2652

Closed
3 tasks
slzhffktm opened this issue Jun 27, 2024 · 3 comments

Comments

@slzhffktm
Copy link

slzhffktm commented Jun 27, 2024

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

{"field1": "somevalue"}

The code

request := make(map[string]interface{})
if err := c.Bind(&request); err != nil {
	return err
}

fmt.Printf("%#v", request)

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

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

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

package main

func main() {
}

Version/commit

v4.12.0

@slzhffktm slzhffktm changed the title Breaking Change on v4.12.0 When Binding to map[string]interface{} Breaking Change on v4.12.0 When Binding to map[string]interface{} -> the field become list Jun 27, 2024
@thesaltree
Copy link
Contributor

@slzhffktm I've submitted a pull request (#2656) that addresses this issue by maintaining backwards compatibility for map[string]interface{} binding while supporting the new functionality.

The PR modifies the bindData function to:

  • Store single values as strings
  • Store multiple values as []string

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 map[string]interface{} returned by c.Bind() and converts any []string values to single strings if they contain only one element.

@slzhffktm
Copy link
Author

Hello @thesaltree ! Seems like your PR is merged, thank you for your contribution!!

@arigon
Copy link

arigon commented Nov 7, 2024

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
}

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.
I'm wondering when this fix will be released, so until now I'm relying on this helper function.

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

No branches or pull requests

3 participants