From 9724592697efed684d20ee9fd6b89782436526d7 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 18 Jun 2020 23:53:53 +0300 Subject: [PATCH] fix https://github.com/kataras/iris/issues/1536#issuecomment-646258752 Former-commit-id: 129d115937617e4d77b7e6e7efddf3168b15d021 --- hero/handler.go | 5 ++++ hero/struct.go | 3 ++- mvc/controller_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/hero/handler.go b/hero/handler.go index 225b1816b..113a7017b 100644 --- a/hero/handler.go +++ b/hero/handler.go @@ -114,6 +114,11 @@ func makeHandler(fn interface{}, c *Container, paramsCount int) context.Handler inputs[binding.Input.Index] = input } + // fmt.Printf("For func: %s | valid input deps length(%d)\n", typ.String(), len(inputs)) + // for idx, in := range inputs { + // fmt.Printf("[%d] (%s) %#+v\n", idx, in.Type().String(), in.Interface()) + // } + outputs := v.Call(inputs) if err := dispatchFuncResult(ctx, outputs, resultHandler); err != nil { c.GetErrorHandler(ctx).HandleError(ctx, err) diff --git a/hero/struct.go b/hero/struct.go index 4ab3eb8e5..b466e5d1a 100644 --- a/hero/struct.go +++ b/hero/struct.go @@ -115,7 +115,8 @@ func (s *Struct) Acquire(ctx context.Context) (reflect.Value, error) { } ctrl := ctx.Controller() - if ctrl.Kind() == reflect.Invalid { + if ctrl.Kind() == reflect.Invalid || + ctrl.Type() != s.ptrType /* in case of changing controller in the same request (see RouteOverlap feature) */ { ctrl = reflect.New(s.elementType) ctx.Values().Set(context.ControllerContextKey, ctrl) elem := ctrl.Elem() diff --git a/mvc/controller_test.go b/mvc/controller_test.go index ecf4ba8d7..4e70832f3 100644 --- a/mvc/controller_test.go +++ b/mvc/controller_test.go @@ -659,3 +659,55 @@ func TestApplicationDependency(t *testing.T) { e.GET("/").Expect().Status(httptest.StatusOK).Body().Equal("app1") e.GET("/other").Expect().Status(httptest.StatusOK).Body().Equal("app2") } + +// Authenticated type. +type Authenticated int64 + +// BasePublicPrivateController base controller between public and private controllers. +type BasePublicPrivateController struct { + CurrentUserID Authenticated + Ctx iris.Context +} + +type publicController struct { + Ctx iris.Context +} + +// Get desc +// Route / [GET] +func (c *publicController) Get() iris.Map { + return iris.Map{"data": "things"} +} + +// privateController serves the "public-private" Customer API. +type privateController struct{ BasePublicPrivateController } + +// Get desc +// Route / [GET] +func (c *privateController) Get() iris.Map { + return iris.Map{"id": c.CurrentUserID} +} + +func TestControllerOverlapping(t *testing.T) { + app := iris.New() + + m := New(app) + m.Router.SetRegisterRule(iris.RouteOverlap) + + m.Register(func(ctx iris.Context) Authenticated { + if ctx.URLParam("name") == "kataras" { + return 1 + } + + ctx.StopWithStatus(iris.StatusForbidden) + return -1 + }) + + // Order matters. + m.Handle(new(privateController)) + m.Handle(new(publicController)) + + e := httptest.New(t, app) + e.GET("/").WithQuery("name", "kataras").Expect().Status(httptest.StatusOK).JSON().Equal(iris.Map{"id": 1}) + e.GET("/").Expect().Status(httptest.StatusOK).JSON().Equal(iris.Map{"data": "things"}) +}