You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With issues:
Inconsistent Middleware Application Based on Route Registration Order
Description
Question 1: Method 1 and Method 2 only differ by the order of registration;Why do Method 1 and Method 2 produce different results? Are these methods legal? Or is this a bug?
Question 2: Isn't Method 3 a bit redundant since the same route group is written twice?
How to reproduce
package main
import (
"github.com/gin-gonic/gin""net/http"
)
typeregisterFuncfunc(*gin.RouterGroup) // Type for route registration functionsvarregisterFuncSlice []registerFunc// Slice of all route registration functionsfuncinit() {
// Register routesregisterFuncSlice=append(registerFuncSlice, fooRoutes)
}
funcfooHandler(c*gin.Context) {
key:=c.GetString("testKey")
c.JSON(http.StatusOK, gin.H{"testValue": key})
c.Abort()
}
functestMiddleware() gin.HandlerFunc {
returnfunc(c*gin.Context) {
c.Set("testKey", "test")
c.Next()
}
}
funcfooRoutes(v1*gin.RouterGroup) {
// Method 1: If fooPrivateRoutes is placed before fooPublicRoutes, fooPublicRoutes// will apply the testMiddleware (not expected)// Accessing http://127.0.0.1:8888/v1/test/no-test-data returns {"testValue":"test"}foo:=v1.Group("/test")
fooPrivateRoutes(foo)
fooPublicRoutes(foo)
//// Method 2: If fooPrivateRoutes is placed after fooPublicRoutes, fooPublicRoutes//// will not apply the testMiddleware (expected)//// Accessing http://127.0.0.1:8888/v1/test/no-test-data returns {"testValue":""}//foo := v1.Group("/test")//fooPublicRoutes(foo)//fooPrivateRoutes(foo)//// Method 3: Separate the two, regardless of the order (expected)//// Accessing http://127.0.0.1:8888/v1/test/no-test-data returns {"testValue":""}//fooPrivate := v1.Group("/test")//fooPrivateRoutes(fooPrivate)////fooPublic := v1.Group("/test")//fooPublicRoutes(fooPublic)/* Question 1: Method 1 and Method 2 only differ by the order of registration; Why do Method 1 and Method 2 produce different results? Are these methods legal? Or is this a bug? Question 2: Isn't Method 3 a bit redundant since the same route group is written twice? */
}
funcfooPublicRoutes(group*gin.RouterGroup) {
group.GET("/no-test-data", fooHandler)
}
funcfooPrivateRoutes(group*gin.RouterGroup) {
test:=group.Use(testMiddleware())
test.GET("/has-test-data", fooHandler)
}
funcsetupRouter() *gin.Engine {
e:=gin.Default()
v1:=e.Group("v1") // Register route group// Dynamically register all routesfor_, regFunc:=rangeregisterFuncSlice {
regFunc(v1)
}
iferr:=e.Run("127.0.0.1:8888"); err!=nil {
panic(err)
}
returne
}
funcmain() {
setupRouter()
}
Inconsistent Middleware Application Based on Route Registration Order
Description
How to reproduce
Expectations
Actual result
Environment
The text was updated successfully, but these errors were encountered: