-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Context Bind: Regression when parsing arrays from multipart/form-data #2731
Comments
This comment was marked as outdated.
This comment was marked as outdated.
ok, got it working with this package main
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/require"
)
func TestEchoBinder(t *testing.T) {
bodyBuffer := new(bytes.Buffer)
mw := multipart.NewWriter(bodyBuffer)
mw.WriteField("ima_slice", "WEBHOOK")
mw.WriteField("ima_slice", "OTHER")
mw.Close()
body := bodyBuffer.Bytes()
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(body))
rec := httptest.NewRecorder()
contentType := mw.FormDataContentType()
req.Header.Set(echo.HeaderContentType, contentType)
ctx := echo.New().NewContext(req, rec)
// Try to bind the request to a map of any
data := map[string]interface{}{}
err := ctx.Bind(&data)
require.Nil(t, err)
// Verify that the "ima_slice" value is actually a slice
// This fails on version 4.13.2 but passes on version 4.12.0
require.Equal(t, []string{"WEBHOOK", "OTHER"}, data["ima_slice"])
} and the PR which cause this is #2656 |
So in v4.11.4 we had different behavior |
@benpate , would changing target type to |
Hey @aldas - thanks for looking into this. Unfortunately, I can't really use a |
if you are always dealing with multipart forms you could use data := map[string]any{}
ctx.Request().ParseMultipartForm(32 << 20)
for k, v := range ctx.Request().MultipartForm.Value {
data[k] = v
} |
Thank you, yes, I think that's where I'm headed. Re-reading my code, I don't necessarily need to put things into a generic map first. So, I'm looking at pulling out |
Issue Description
The code below works in echo v4.12.0, but breaks in v4.13.2. Previously, when reading a request body encoded with
multipart/form-data
, multiple values for a single variable name would be returned as a slice of strings. Now, in v4.13.2, only the first value is returned.I believe there was some work around this area with the 4.13.0 release but I haven't dug into the code enough to say what's causing the issue.
This is a problem because it silently broke parts of my application that were expecting a slice but received something else.
Anyway, test case:
Working code to debug
Version/commit
The text was updated successfully, but these errors were encountered: