dependency injection: fix read slices

rel: https://github.com/kataras/iris/issues/1542

Former-commit-id: 0028fafa60b80c80cade1e7a18a11109ce0e9948
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-06-20 07:22:29 +03:00
parent 311b560717
commit 3c86fa8010
2 changed files with 34 additions and 8 deletions

View File

@ -209,7 +209,7 @@ func getBindingsFor(inputs []reflect.Type, deps []*Dependency, paramsCount int)
}
// else add builtin bindings that may be registered by user too, but they didn't.
if indirectType(in).Kind() == reflect.Struct {
if isPayloadType(in) {
bindings = append(bindings, payloadBinding(i, in))
continue
}
@ -219,6 +219,15 @@ func getBindingsFor(inputs []reflect.Type, deps []*Dependency, paramsCount int)
return
}
func isPayloadType(in reflect.Type) bool {
switch indirectType(in).Kind() {
case reflect.Struct, reflect.Slice:
return true
default:
return false
}
}
func getBindingsForFunc(fn reflect.Value, dependencies []*Dependency, paramsCount int) []*binding {
fnTyp := fn.Type()
if !isFunc(fnTyp) {
@ -331,9 +340,15 @@ func payloadBinding(index int, typ reflect.Type) *binding {
}
}
if input.Type.Kind() == reflect.Slice {
newValue = reflect.New(reflect.SliceOf(indirectType(input.Type)))
} else {
newValue = reflect.New(indirectType(input.Type))
}
ptr := newValue.Interface()
err = ctx.ReadBody(ptr)
if !wasPtr {
newValue = newValue.Elem()
}

View File

@ -711,23 +711,34 @@ func TestControllerOverlapping(t *testing.T) {
type testControllerMethodHandlerBindStruct struct{}
type queryData struct {
type bindStructData struct {
Name string `json:"name" url:"name"`
}
func (*testControllerMethodHandlerBindStruct) Any(data queryData) queryData {
func (*testControllerMethodHandlerBindStruct) Any(data bindStructData) bindStructData {
return data
}
func (*testControllerMethodHandlerBindStruct) PostByProducts(id uint64, data []bindStructData) []bindStructData {
return data
}
func TestControllerMethodHandlerBindStruct(t *testing.T) {
app := iris.New()
New(app).Handle(new(testControllerMethodHandlerBindStruct))
m := New(app.Party("/data"))
m.HandleError(func(ctx iris.Context, err error) {
t.Fatalf("Path: %s, Error: %v", ctx.Path(), err)
})
data := iris.Map{"name": "kataras"}
m.Handle(new(testControllerMethodHandlerBindStruct))
data := bindStructData{Name: "kataras"}
manyData := []bindStructData{{"kataras"}, {"john doe"}}
e := httptest.New(t, app)
e.GET("/").WithQueryObject(data).Expect().Status(httptest.StatusOK).JSON().Equal(data)
e.PATCH("/").WithJSON(data).Expect().Status(httptest.StatusOK).JSON().Equal(data)
e.GET("/data").WithQueryObject(data).Expect().Status(httptest.StatusOK).JSON().Equal(data)
e.PATCH("/data").WithJSON(data).Expect().Status(httptest.StatusOK).JSON().Equal(data)
e.POST("/data/42/products").WithJSON(manyData).Expect().Status(httptest.StatusOK).JSON().Equal(manyData)
// more tests inside the hero package itself.
}