Skip to content

Middleware Use behavior undocumented and unintuitive #2010

@sguillia

Description

@sguillia

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"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions