Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]Dependencies injection failed #1468

Closed
rocinantex opened this issue Mar 14, 2020 · 2 comments · Fixed by #1474
Closed

[BUG]Dependencies injection failed #1468

rocinantex opened this issue Mar 14, 2020 · 2 comments · Fixed by #1474

Comments

@rocinantex
Copy link
Contributor

rocinantex commented Mar 14, 2020

Dependencies injection failed

To Reproduce
The following code will be reproduce this bug

package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"log"
)

type Service struct{}

func (s *Service) DoSomething(ctx iris.Context) {
	if _, err := ctx.WriteString("Foo bar"); err != nil {
		log.Println(err)
	}
}

type Controller struct {
	Ctx     iris.Context
	Service *Service
}

type FooController struct {
	Controller
}

type BarController struct {
	FooController
}

type FinalController struct {
	BarController
}

func (c *FinalController) Get() {
	// c.Ctx and c.Service are not injected
	// this action will trigger a panic
	c.Service.DoSomething(c.Ctx)
}

func main() {
	app := iris.New()
	m := mvc.New(app)
	m.Register(new(Service))
	m.Handle(new(FinalController))
	if err := app.Listen(":8080"); err != nil {
		log.Fatal(err)
	}
}

Desktop:

  • OS: ubuntu 18.04

After debugging I found a possible cause of this bug
struct.go
reflect.go
In my gist case, the field.Index of all fields returned by LookupNonZeroFieldsValues will be come the last field.Index value

Then I try to fix this bug, I added the following code in
reflect.go

tmp := make([]int, len(index))
copy(tmp, index)
field := field{
    Type:   f.Type,
    Name:   f.Name,
    Index:  tmp,
    CanSet: isExported,
}		

Then the bug disappeared, but I still don't know why.
Because index is a local variable, why does it cause all fields to be modified?

@kataras
Copy link
Owner

kataras commented Mar 14, 2020

Hello @rocinantex, the dependency injection code is fully re-written in the upcoming version of Iris, see the master branch. Try it, (see the new examples as well), and if your code still failing please post the code snippet in order to provide you more help, don't try to fix it alone, just share a re-producible code.

@rocinantex
Copy link
Contributor Author

rocinantex commented Mar 14, 2020

@kataras I just tried it with the same code in master branch and still injection failed.

I modified reflect.go, then it worked.

tmp := make([]int, len(index))
copy(tmp, index)
field.Index = tmp

@kataras kataras added this to the v12.2.0 milestone Mar 22, 2020
@kataras kataras linked a pull request Mar 22, 2020 that will close this issue
kataras added a commit that referenced this issue Mar 24, 2020
kataras added a commit that referenced this issue Jul 26, 2020
Former-commit-id: 3e7d927761a5d5559b65ea3f91b94e3dc523a187
@kataras kataras closed this as completed Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants