From df3a68255c6c28b0e37bb00540437315fe226881 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 1 Mar 2019 14:10:07 +0200 Subject: [PATCH] fix https://github.com/kataras/iris/issues/1205 Former-commit-id: d95be1456a78fbafd7ec5fec22f2066454eb76c6 --- hero/hero.go | 2 +- mvc/controller_handle_test.go | 11 ++++++++++- mvc/param.go | 23 ++++++++++++++--------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/hero/hero.go b/hero/hero.go index 9862816d..dfd6e873 100644 --- a/hero/hero.go +++ b/hero/hero.go @@ -7,7 +7,7 @@ import ( "github.com/kataras/iris/context" ) -// def is the default herp value which can be used for dependencies share. +// def is the default hero value which can be used for dependencies share. var def = New() // Hero contains the Dependencies which will be binded diff --git a/mvc/controller_handle_test.go b/mvc/controller_handle_test.go index 9864b3ad..0b1a3a6f 100644 --- a/mvc/controller_handle_test.go +++ b/mvc/controller_handle_test.go @@ -97,12 +97,19 @@ func (c *testControllerHandle) HiParamEmptyInputBy() string { return "empty in but served with ctx.Params.Get('ps')=" + c.Ctx.Params().Get("ps") } +type testSmallController struct{} + +// test ctx + id in the same time. +func (c *testSmallController) GetHiParamEmptyInputWithCtxBy(ctx context.Context, id string) string { + return "empty in but served with ctx.Params.Get('param2')= " + ctx.Params().Get("param2") + " == id == " + id +} + func TestControllerHandle(t *testing.T) { app := iris.New() - m := New(app) m.Register(&TestServiceImpl{prefix: "service:"}) m.Handle(new(testControllerHandle)) + m.Handle(new(testSmallController)) e := httptest.New(t, app) @@ -130,4 +137,6 @@ func TestControllerHandle(t *testing.T) { Body().Equal("value") e.GET("/hiparamempyinput/value").Expect().Status(httptest.StatusOK). Body().Equal("empty in but served with ctx.Params.Get('ps')=value") + e.GET("/hi/param/empty/input/with/ctx/value").Expect().Status(httptest.StatusOK). + Body().Equal("empty in but served with ctx.Params.Get('param2')= value == id == value") } diff --git a/mvc/param.go b/mvc/param.go index faa68396..c7bdae57 100644 --- a/mvc/param.go +++ b/mvc/param.go @@ -35,16 +35,21 @@ func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type) // } // } - for i, param := range params { - if len(funcIn) <= i { - return - } - funcDep, ok := context.ParamResolverByTypeAndIndex(funcIn[i], param.Index) - if !ok { - continue - } + consumed := make(map[int]struct{}) + for _, in := range funcIn { + for j, param := range params { + if _, ok := consumed[j]; ok { + continue + } + funcDep, ok := context.ParamResolverByTypeAndIndex(in, param.Index) + if !ok { + continue + } - values = append(values, funcDep) + values = append(values, funcDep) + consumed[j] = struct{}{} + break + } } return