Skip to content

Group middleware does not work for "Any" route #2517

Closed

Description

@KaymeKaydex

Issue Description

Checklist

  • [ + ] Dependencies installed
  • [ + ] No typos
  • [ + ] Searched existing issues and docs

Expected behaviour

normal routing

Actual behaviour

{
"message": "Not Found"
}

Steps to reproduce

just create any request

Working code to debug

	g := e.Group("/api/test") 
	g.Use(echo.WrapMiddleware(mw.NewMW()))
	{
		g.Any("*", testController.Proxy) 
	}

actual result is 
{
    "message": "Not Found"
}
but route cant return anything

Version/commit

github.com/labstack/echo/v4 v4.11.1

Activity

  1. changed the title [-]group middleware does not work for any route[/-] [+]Group middleware does not work for "Any" route[/+] on Sep 6, 2023
  2. aldas commented on Sep 6, 2023

    @aldas
    Contributor

    This is little bit tricky.

    Let assume group and handler is added like this:

    func main() {
    	e := echo.New()
    
    	g := e.Group("/api/test")
    	//g.Use(middleware.Logger())
    	g.Any("*", func(c echo.Context) error {
    		return c.String(http.StatusOK, c.Path())
    	})
    
    	if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
    		e.Logger.Fatal(err)
    	}
    }

    When you add route with g.Any("*" you will end up with route path /api/test*. This will match both of following request

    x@x:~/code$ curl http://localhost:8080/api/test/hi
    /api/test*
    x@x:~/code$ curl http://localhost:8080/api/testhi
    /api/test*

    as your router has following routes
    image

    now when we add middleware to that group by uncommenting g.Use(middleware.Logger())

    we have following routes
    image

    that 4 is 404 route for path /api/test/* which is causing problem here as it is more precise match than api/test*.

    x@x:~/code$ curl http://localhost:8080/api/testhi
    /api/test*
    x@x:~/code$ curl http://localhost:8080/api/test/hi
    {"message":"Not Found"}

    As a workaround I suggest:

    • adding that any route like that g.Any("/*" as it will then overwrite that 404 route.
    • or creating group like that g := e.Group("/api/test/")

    That 404 route is a "feature" that is added here (line 32)

    echo/group.go

    Lines 21 to 32 in 77d5ae6

    func (g *Group) Use(middleware ...MiddlewareFunc) {
    g.middleware = append(g.middleware, middleware...)
    if len(g.middleware) == 0 {
    return
    }
    // group level middlewares are different from Echo `Pre` and `Use` middlewares (those are global). Group level middlewares
    // are only executed if they are added to the Router with route.
    // So we register catch all route (404 is a safe way to emulate route match) for this group and now during routing the
    // Router would find route to match our request path and therefore guarantee the middleware(s) will get executed.
    g.RouteNotFound("", NotFoundHandler)
    g.RouteNotFound("/*", NotFoundHandler)
    }

  3. vishr commented on Jun 13, 2026

    @vishr
    Member

    Thanks for reporting this. It's been inactive for a long time and may already be resolved in a current release (v5 / v4.15.x). I'm closing to keep the tracker focused — if you still hit this on a current version, please comment with a minimal reproduction and we'll reopen right away. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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