Skip to content

🤗 [Question]: Grouping middleware has some counterintuitive issues #3639

@1514599514

Description

@1514599514

Question Description

Image

Requesting a non-existent URL will also trigger the middleware for prefix matching. At present, I haven't thought of any serious bugs that this issue might cause. However, I believe that it is more intuitive for the middleware to be triggered only after the route matching is successful, and there won't be any performance issues either. After all, all the middleware that will be triggered by any URL is fixed after the startup.

Another issue is that /v11 will trigger the middleware of /v1. This is bound to catch someone off guard.

The above is the translation.

请求一个不存在的URL同样会触发前缀匹配的中间件.
目前没想到这个问题会导致的恶性BUG, 但是我想路由匹配成功后触发中间件才符合直觉, 且不会有啥性能问题, 毕竟所有URL会触发的中间件在启动后, 就已经固定了.

另一个就是老问题了, /v11 会触发 /v1的中间件.
这个肯定会有人被坑的.

Code Snippet (optional)

package main

import (
	"fmt"
	"net/http/httptest"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/logger"
)

func main() {
	app := fiber.New()

	app.Use(logger.New())

	api := app.Group("/api", func(c fiber.Ctx) error {
		fmt.Println("Group /api")
		return c.Next()
	})

	v1 := api.Group("/v1", func(c fiber.Ctx) error {
		fmt.Println("Group /api/v1")
		return c.Next()
	})
	v1.Get("/list", handler) // /api/v1/list
	v1.Get("/user", handler) // /api/v1/user

	v11 := api.Group("/v11", func(c fiber.Ctx) error {
		fmt.Println("Group /api/v11")
		return c.Next()
	}) // /api/v11
	v11.Get("/list", handler) // /api/v11/list
	v11.Get("/user", handler) // /api/v11/user

	req := httptest.NewRequest("GET", "http://google.com/api/v1/login", nil)
	app.Test(req)

	req = httptest.NewRequest("GET", "http://google.com/api/v11/logout", nil)
	app.Test(req)
}

func handler(c fiber.Ctx) error {
	return c.SendString("handler")
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions