Skip to content

Commit

Permalink
🐛 bug: fix route naming issue when using same path for different methods
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Oct 21, 2023
1 parent 37ad7c7 commit 1dc9a5e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,11 @@ func (app *App) Name(name string) Router {

for _, routes := range app.stack {
for _, route := range routes {
if route.Path == app.latestRoute.Path {
isMethodValid := route.Method == app.latestRoute.Method || app.latestRoute.use ||
(app.latestRoute.Method == MethodGet && route.Method == MethodHead)

if route.Path == app.latestRoute.Path && isMethodValid {

Check warning on line 626 in app.go

View workflow job for this annotation

GitHub Actions / lint

empty-lines: extra empty line at the start of a block (revive)

route.Name = name

if route.group != nil {
Expand Down
20 changes: 19 additions & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ func Test_Middleware_Route_Naming_With_Use(t *testing.T) {
}
}

func Test_Route_Naming_Issue_2671(t *testing.T) {
func Test_Route_Naming_Issue_2671_2685(t *testing.T) {
app := New()

app.Get("/", emptyHandler).Name("index")
Expand Down Expand Up @@ -1904,4 +1904,22 @@ func Test_Route_Naming_Issue_2671(t *testing.T) {

postGroup.Post("", emptyHandler).Name("post.update")
utils.AssertEqual(t, "/post/:postId", app.GetRoute("post.update").Path)

app.Get("/users", nil).Name("get-users")
app.Post("/users", nil).Name("add-user")
getUsers := app.GetRoute("get-users")
utils.AssertEqual(t, getUsers.Path, "/users")

addUser := app.GetRoute("add-user")
utils.AssertEqual(t, addUser.Path, "/users")

newGrp := app.Group("/name-test")

newGrp.Get("/users", nil).Name("get-users")
newGrp.Post("/users", nil).Name("add-user")
getUsers = app.GetRoute("get-users")
utils.AssertEqual(t, getUsers.Path, "/users")

addUser = app.GetRoute("add-user")
utils.AssertEqual(t, addUser.Path, "/users")
}

0 comments on commit 1dc9a5e

Please sign in to comment.