Description

When creating a IRoute with middleware like this

route := ginEngine.GET("/test", func(ctx *gin.Context) { /**/) })
route.Use( someHandlerFunc() )

the middleware doesn't trigger, or triggers when it shouldn't.

I register routes for a generic class, so that i have some default endpoints per type. For some types i want to be able to modify these default endpoints and tried to do this via middleware. When using Use() on IRoute or even RouterGroup objects, it breaks when being added after its respective ginengine.post calls. They won't trigger at all, or trigger for the wrong route

Expectations

I can add HandlerFuncs to any IRoute at any time in the code and it gets executed before the handlerFunc provided by the ginEngine.POST/GET/... calls

Actual result

Registering Handler Funcs directly to the return value of ginEngine.POST/GET/... calls registers the Handler in some random handlers but not the desired Route

Environment

  • go version: 1.23.3
  • gin version (or commit ref): 1.10.0
  • operating system: Windows

Comment From: VarusHsu

route.Use( someHandlerFunc() ) should before route := ginEngine.GET("/test", func(ctx *gin.Context) { /**/) })

Comment From: simon-winter

Yes, but this shouldn't be like that. Router get handeld later, so order of registration should not Matter. Also adding in Middleware after the handlers leads to undefined behavior which is also odd