Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
val.Set(reflect.MakeMap(typ))
}
for k, v := range data {
if len(v) == 0 {
// skip entries with no values to avoid panicking on v[0]
continue
}
if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else if isElemInterface {
Expand Down Expand Up @@ -322,6 +326,10 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m
if !exists {
continue
}
if len(inputValue) == 0 {
// skip entries with no values to avoid panicking on inputValue[0]
continue
}

// NOTE: algorithm here is not particularly sophisticated. It probably does not work with absurd types like `**[]*int`
// but it is smart enough to handle niche cases like `*int`,`*[]string`,`[]*int` .
Expand Down
30 changes: 30 additions & 0 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1873,3 +1873,33 @@ func TestOpenAPIFormatTimeBindingEdgeCases(t *testing.T) {
})
}
}

func TestBindHeadersEmptyValues(t *testing.T) {
// Regression test for https://github.com/labstack/echo/issues/3169
// BindHeaders must not panic when a header value is a nil or empty []string.
for _, values := range [][]string{nil, {}} {
t.Run(fmt.Sprintf("values=%v", values), func(t *testing.T) {
e := New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header["X-Empty"] = values
c := e.NewContext(req, httptest.NewRecorder())

var dst struct {
Value string `header:"X-Empty"`
}
if err := BindHeaders(c, &dst); err != nil {
t.Errorf("BindHeaders: %v", err)
}

var m map[string]string
if err := BindHeaders(c, &m); err != nil {
t.Errorf("BindHeaders(map[string]string): %v", err)
}

var anyMap map[string]any
if err := BindHeaders(c, &anyMap); err != nil {
t.Errorf("BindHeaders(map[string]any): %v", err)
}
})
}
}