Skip to content

Binding into a pointer to a slice returns an error "unknown type" #2381

@maticmeznar

Description

@maticmeznar

Issue Description

When binding input data into a struct with a field with a pointer to a slice, *[]string for example, Bind method returns "unknown type" error.

Checklist

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

Expected behaviour

Successfully bind input data into the provided struct

Actual behaviour

Bind method returns "unknown type" error

Steps to reproduce

package echotest

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/labstack/echo/v4"
	"github.com/stretchr/testify/assert"
)

type (
	InputData struct {
		Tags *[]string `json:"tags" query:"tags"`
	}

	handler struct{}
)

func (h *handler) testHandler1(c echo.Context) error {
	data := new(InputData)
	if err := c.Bind(data); err != nil {
		return err
	}

	spew.Dump(data)

	return c.JSON(http.StatusOK, data)
}

func TestTest(t *testing.T) {
	e := echo.New()

	q := make(url.Values)
	q.Add("tags", "testing")
	q.Add("tags", "server")

	req := httptest.NewRequest(http.MethodGet, "/?"+q.Encode(), nil)
	// req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)

	h := &handler{}
	if assert.NoError(t, h.testHandler1(c)) {
		assert.Equal(t, http.StatusOK, rec.Code)
	}
}

Version/commit

v4.9.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions