mirror of
https://github.com/kataras/iris.git
synced 2025-03-21 12:46:27 +01:00
dependency injection: fix read slices
rel: https://github.com/kataras/iris/issues/1542 Former-commit-id: 0028fafa60b80c80cade1e7a18a11109ce0e9948
This commit is contained in:
parent
311b560717
commit
3c86fa8010
|
@ -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.
|
// 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))
|
bindings = append(bindings, payloadBinding(i, in))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -219,6 +219,15 @@ func getBindingsFor(inputs []reflect.Type, deps []*Dependency, paramsCount int)
|
||||||
return
|
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 {
|
func getBindingsForFunc(fn reflect.Value, dependencies []*Dependency, paramsCount int) []*binding {
|
||||||
fnTyp := fn.Type()
|
fnTyp := fn.Type()
|
||||||
if !isFunc(fnTyp) {
|
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))
|
newValue = reflect.New(indirectType(input.Type))
|
||||||
|
}
|
||||||
|
|
||||||
ptr := newValue.Interface()
|
ptr := newValue.Interface()
|
||||||
err = ctx.ReadBody(ptr)
|
err = ctx.ReadBody(ptr)
|
||||||
|
|
||||||
if !wasPtr {
|
if !wasPtr {
|
||||||
newValue = newValue.Elem()
|
newValue = newValue.Elem()
|
||||||
}
|
}
|
||||||
|
|
|
@ -711,23 +711,34 @@ func TestControllerOverlapping(t *testing.T) {
|
||||||
|
|
||||||
type testControllerMethodHandlerBindStruct struct{}
|
type testControllerMethodHandlerBindStruct struct{}
|
||||||
|
|
||||||
type queryData struct {
|
type bindStructData struct {
|
||||||
Name string `json:"name" url:"name"`
|
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
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestControllerMethodHandlerBindStruct(t *testing.T) {
|
func TestControllerMethodHandlerBindStruct(t *testing.T) {
|
||||||
app := iris.New()
|
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 := httptest.New(t, app)
|
||||||
e.GET("/").WithQueryObject(data).Expect().Status(httptest.StatusOK).JSON().Equal(data)
|
e.GET("/data").WithQueryObject(data).Expect().Status(httptest.StatusOK).JSON().Equal(data)
|
||||||
e.PATCH("/").WithJSON(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.
|
// more tests inside the hero package itself.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user