-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Closed
Labels
Description
Issue Description
When posting a struct that includes a field that implements Echo#BindUnmarshaler, it doesn't seem to call UnmarshalParam for it when calling c.Bind. It does however work on GET with query parameters (there is a test for this already).
Checklist
- Dependencies installed
- No typos
- Searched existing issues and docs
Expected behaviour
I'd expect Echo to call UnmarshalParam when Binding to a struct. The documentation at https://echo.labstack.com/guide/request gives an example for a Timestamp but it doesn't include the struct that was being used. It might be nice to include that in a future version.
Actual behaviour
Given the test below (added to bind_test.go) I get the following error:
--- FAIL: TestPostBindUnmarshalParam (0.00s)
bind_test.go:288:
Error Trace: bind_test.go:288
Error: Received unexpected error:
code=400, message=Unmarshal type error: expected=echo.Timestamp, got=string, field=ts, offset=30, internal=json: cannot unmarshal string into Go struct field .ts of type echo.Timestamp
Test: TestPostBindUnmarshalParam
Steps to reproduce
Add the test below to bind_test.go and run the test.
Working code to debug
func TestPostBindUnmarshalParam(t *testing.T) {
e := New()
body := bytes.NewBufferString(`{ "ts": "1970-12-06T19:09:05Z" }`)
req := httptest.NewRequest(http.MethodPost, "/", body)
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
result := struct {
T Timestamp `json:"ts"`
}{}
err := c.Bind(&result)
spew.Dump(result)
ts := Timestamp(time.Date(1970, 12, 6, 19, 9, 5, 0, time.UTC))
assert := assert.New(t)
if assert.NoError(err) {
assert.Equal(ts, result.T)
}
}
Version/commit
❯ go version
go version go1.13 darwin/amd64
Tested with Echo commit 81a6608.