labstack / echo

High performance, minimalist Go web framework

Home Page:https://echo.labstack.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bind() panics when the destination is a map with non-interface types

astromechza opened this issue · comments

Issue Description

When calling context Bind(...) with a map which has non interface or non assignable value type, a panic is triggered. Even though this is a rare case, and can be worked around using a different map type, or using BindBody, we should not have panics in echo. This looks like it has been the behavior since 2019 or so based on git blame.

This comes down to the reflect assignment here:

echo/bind.go

Line 137 in 584cb85

val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))

func (b *DefaultBinder) bindData(destination interface{}, data map[string][]string, tag string) error {
...
	// Map
	if typ.Kind() == reflect.Map {
		for k, v := range data {
			val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
		}
		return nil
	}
...

I couldn't find an issue for this elsewhere.

Expected behaviour

Assignable values are bound. Unassignable values are ignored.

Actual behaviour

It panics!

For example:

panic: reflect.Value.SetMapIndex: value of type string is not assignable to type int

Steps to reproduce

See the code below. Call Bind() with a map[string]int and some string query or path parameter available in the context.

Working code to debug

package main

import (
	"github.com/labstack/echo/v4"
	"net/http"
)

type TestBodyMapInt map[string]int

func main() {
	e := echo.New()

	// test: curl -XPOST --header "Content-Type: application/json" -d '{"module1": 2, "module2": 3}'  http://127.0.0.1:8080/test/1
	e.POST("/test/:id", func(c echo.Context) error {
		p := TestBodyMapInt{}
		if err := c.Bind(&p); err != nil {
			return err
		}
		return c.JSON(http.StatusOK, p)
	})

	e.Start("127.0.0.1:8080")
}

Version/commit

github.com/labstack/echo/v4 v4.11.3

Relates to #988

This seems to be old unresolved problem. I think we can make it work so binding to map[string]string and map[string][]string works. As the source for the values we are binding is Path params, Headers, Query params, Form params and all of these are string values. For all other map cases we should error out or skip in case of source being "param/query/header/form".