gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.2k stars 1.64k forks source link

🐛 [Bug]: Naming of routes works wrong #2685

Closed Antonov-guap closed 11 months ago

Antonov-guap commented 11 months ago

Bug Description

Naming of routes with the same path and different methods works wrong. All routes with the same path are replaced with new given name.

How to Reproduce

Steps to reproduce the behavior:

  1. Create new app:

    app := fiber.New()
    app.Get("/users", nil).Name("get-users")
    app.Post("/users", nil).Name("add-user")
  2. Try to get route by name:

    c := app.AcquireCtx(&fasthttp.RequestCtx{})
    defer app.ReleaseCtx(c)
    location, err := c.GetRouteURL("get-users", fiber.Map{})
  3. Check the result:

    fmt.Println("location:", location)
    fmt.Println("err:", err)
  4. Actual result:

    location: 
    err: <nil>

Expected Behavior

Expected that path will be "/users" with no error:

location: /users
err: <nil>

Fiber Version

v2.50.0

Code Snippet (optional)

package main

import (
    "fmt"

    "github.com/gofiber/fiber/v2"
    "github.com/valyala/fasthttp"
)

func main() {
    app := fiber.New()
    app.Get("/users", nil).Name("get-users")
    app.Post("/users", nil).Name("add-user")

    c := app.AcquireCtx(&fasthttp.RequestCtx{})
    defer app.ReleaseCtx(c)
    location, err := c.GetRouteURL("get-users", fiber.Map{})

    fmt.Println("location:", location)
    fmt.Println("err:", err)

    getUsers := app.GetRoute("get-users")
    addUser := app.GetRoute("add-user")

    fmt.Println("get-users:", getUsers.Method, getUsers.Path)
    fmt.Println("add-user:", addUser.Method, addUser.Path)
}

// Output: 
// location: 
// err: <nil>
// get-users:  
// add-user: GET /users

Checklist:

welcome[bot] commented 11 months ago

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

trapcodeio commented 11 months ago

I think this is related to #2671

Antonov-guap commented 11 months ago

yes, related, but issue https://github.com/gofiber/fiber/issues/2671 has test (i saw it). And this test is also has problems. It adds some name, after it asserts, then again add name -> assert. But if you add all the names before, and then assert all, you will get unpredictable result, cos name adding affect all names previously added.

Antonov-guap commented 11 months ago

and also, I didn't make an issue, but when you mount app as subApp, then mount also drops names of routes (when listen started).