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

Middleware Use behavior undocumented and unintuitive #2010

Open
sguillia opened this issue Aug 7, 2019 · 2 comments
Open

Middleware Use behavior undocumented and unintuitive #2010

sguillia opened this issue Aug 7, 2019 · 2 comments

Comments

@sguillia
Copy link

@sguillia sguillia commented Aug 7, 2019

Please take a look at the following code

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.New()

	r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{}) })

	r.Use(gin.Logger())

	r.GET("/pingx", func(c *gin.Context) { c.JSON(200, gin.H{}) })

	r.Run()
}

And now please take a look at the documentation for Use() (emphasis mine):

Use attaches a global middleware to the router. ie. the middleware attached though Use() will be included in the handlers chain for every single request

Who in the world would have guessed that the middlewares are only called with the routes that where declared after them, not before?

Steps to reproduce:

  1. go run main.go with main.go being the code above
  2. curl localhost:8080/pingx and watch as the logger logs requests on stdout
  3. curl localhost:8080/ping and watch as nothing is logged anywhere.

To me this is a bug but I have a feeling that this is intended behavior...

If it is intended, I would suggest to update the documentation, but this is not enough.
I would also change the README because this behavior is documented nowhere.
Also I suggest renaming .Use to .UseFromNowOn or to have both calls available.

This bug prevents from using middlewares with OpenAPI go-gin-server generator . OpenAPI generator is a tool that writes API routes (among others). By nature, a lot of users will add the CORS middleware, and will:

  1. Encounter the bug
  2. Add code in an auto-generated file as a work-around

Also, this thread shows users struggling with the bug. It has been around for years
gin-contrib/cors#29

Thank you for your understanding

  • go version: go1.12.7
  • gin version (or commit ref): v1.4.0-dev
  • operating system: linux/amd64

Note. For some people, it might look intuitive in the code above that Use means UseFromNowOn. But I happened to work on this, where NewRouter is generated code. It is way less explicit.

	router := routers.NewRouter()
	router.Use(cors.Default())
	log.Fatal(router.Run(":8000"))
@simplytunde
Copy link

@simplytunde simplytunde commented Aug 9, 2019

@sguillia I just confirmed this behavior with Use. I think a clearer documentation that points out this behavior will be a better.

@aight8
Copy link

@aight8 aight8 commented Oct 12, 2019

Same for group.Use(...)
The middleware is not global anymore than.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.