Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug]: Router Name abnormality in version v2.47 upwards #2671

Closed
3 tasks done
trapcodeio opened this issue Oct 9, 2023 · 4 comments · Fixed by #2676
Closed
3 tasks done

🐛 [Bug]: Router Name abnormality in version v2.47 upwards #2671

trapcodeio opened this issue Oct 9, 2023 · 4 comments · Fixed by #2676
Assignees

Comments

@trapcodeio
Copy link

Bug Description

Greetings everyone, This is my first issue here.
First and foremost, Big thanks to the contributors on Fiber. I've found it incredibly useful coming from Nodejs, and I appreciate the effort that has gone into it.

I make use of route names heavily in my project and everything worked perfectly until I tried to update from 2.46 to any future version. (I tried all, 2.47 till date. i.e 2.49)

Some route names are registered while some are not.
I have created a mini project that shows this using the EnablePrintRoutes option.

In version 2.46 all route names are registered as expected but 2.47 omits some.

PS: The comments in the code explain some behaviors I noticed

How to Reproduce

Steps to reproduce the behavior:

  1. Run the snippet below using version 2.46 and then run it on 2.47
  2. check the console log to see the printed routes and differences

OR clone this structured repo
https://github.com/trapcodeio/fiber-route-name-bug

It consists of two projects in one folder, one using v2.46 and the other using v2.47
The Server runs on port 3000 and 3001 respectively.

Expected Behavior

2.46

method path name handlers
HEAD / main.handleRequest
GET / index main.handleRequest
GET /a/:a_id a main.handleRequest
HEAD /a/:a_id main.handleRequest
POST /b/:bId b main.handleRequest
GET /c c.get main.handleRequest
HEAD /c main.handleRequest
POST /c c.post main.handleRequest
GET /c/d c.get.d main.handleRequest
HEAD /c/d main.handleRequest
POST /d/:d_id d.post main.handleRequest
GET /d/:d_id d.get main.handleRequest
HEAD /d/:d_id main.handleRequest
GET /e/:eId e.get main.handleRequest
HEAD /e/:eId main.handleRequest
POST /e/:eId e.post main.handleRequest
HEAD /e/:eId/f main.handleRequest
GET /e/:eId/f e.get.f main.handleRequest
HEAD /post/:postId main.handleRequest
GET /post/:postId post.get main.handleRequest
POST /post/:postId post.update main.handleRequest

2.47 upwards

method path name handlers
HEAD / index main.handleRequest
GET / index main.handleRequest
GET /a/:a_id a main.handleRequest
HEAD /a/:a_id a main.handleRequest
POST /b/:bId main.handleRequest
GET /c c.post main.handleRequest
HEAD /c c.post main.handleRequest
POST /c c.post main.handleRequest
GET /c/d c.get.d main.handleRequest
HEAD /c/d c.get.d main.handleRequest
POST /d/:d_id d.post main.handleRequest
GET /d/:d_id d.post main.handleRequest
HEAD /d/:d_id d.post main.handleRequest
GET /e/:eId main.handleRequest
HEAD /e/:eId main.handleRequest
POST /e/:eId main.handleRequest
HEAD /e/:eId/f main.handleRequest
GET /e/:eId/f main.handleRequest
HEAD /post/:postId main.handleRequest
GET /post/:postId main.handleRequest
POST /post/:postId main.handleRequest

Fiber Version

v2.47...

Code Snippet (optional)

package main

import (
	"github.com/gofiber/fiber/v2"
)

func handleRequest(c *fiber.Ctx) error {
	return c.JSON(fiber.Map{
		"method": c.Method(),
		"url":    c.OriginalURL(),
		"name":   c.Route().Name,
		"params": c.AllParams(),
	})
}

func main() {
	app := fiber.New(fiber.Config{
		EnablePrintRoutes: true,
	})

	app.Get("/", handleRequest).Name("index")

	// snake case param works (name is registered)
	app.Get("/a/:a_id", handleRequest).Name("a")
	// camel case param doesn't work (name is not registered)
	app.Post("/b/:bId", handleRequest).Name("b")

	// group without param
	c := app.Group("/c")
	c.Get("", handleRequest).Name("c.get")
	// for some reason `c.get` is not registered and `c.post` is registered
	// if you comment out the `c.post` route, `c.get` will be registered
	c.Post("", handleRequest).Name("c.post")
	// this works as expected
	c.Get("/d", handleRequest).Name("c.get.d")

	// group with params
	d := app.Group("/d/:d_id")
	// works as expected
	d.Get("", handleRequest).Name("d.get")
	// for some reason `d.get` is not registered and `d.post` is registered
	// if you comment out the `d.post` route, `d.get` will be registered
	d.Post("", handleRequest).Name("d.post")

	// group with camel case param
	e := app.Group("/e/:eId")
	// All route names in this group are not registered
	e.Get("", handleRequest).Name("e.get")
	e.Post("", handleRequest).Name("e.post")
	e.Get("f", handleRequest).Name("e.get.f")

	// using real world example
	postGroup := app.Group("/post/:postId")
	// All route names in this group are not registered just like the `e` group
	postGroup.Get("", handleRequest).Name("post.get")
	postGroup.Post("", handleRequest).Name("post.update")

	err := app.Listen(":3001")
	if err != nil {
		panic(err)
	}
}

Checklist:

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

welcome bot commented Oct 9, 2023

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

@ReneWerner87
Copy link
Member

Maybe related to #2477

@efectn @ytsruh can you check

@efectn
Copy link
Member

efectn commented Oct 13, 2023

Will check it today

@trapcodeio
Copy link
Author

Greetings,

This bug still exists.
I have also updated the test repo I provided earlier.

This is what the routes prints after i tried 2.50

method path name handlers
HEAD / index main.handleRequest
GET / index main.handleRequest
GET /a/:a_id a main.handleRequest
HEAD /a/:a_id a main.handleRequest
POST /b/:bId b main.handleRequest
GET /c c.post main.handleRequest
HEAD /c c.post main.handleRequest
POST /c c.post main.handleRequest
GET /c/d c.get.d main.handleRequest
HEAD /c/d c.get.d main.handleRequest
POST /d/:d_id d.post main.handleRequest
GET /d/:d_id d.post main.handleRequest
HEAD /d/:d_id d.post main.handleRequest
GET /e/:eId e.post main.handleRequest
HEAD /e/:eId e.post main.handleRequest
POST /e/:eId e.post main.handleRequest
HEAD /e/:eId/f e.get.f main.handleRequest
GET /e/:eId/f e.get.f main.handleRequest
HEAD /post/:postId post.update main.handleRequest
GET /post/:postId post.update main.handleRequest
POST /post/:postId post.update main.handleRequest

From the table above, we can see that routes with same URL don't use independent names
e.g The GET /c request is supposed to be c.get but got c.post instead.

Also is there any reason why other methods are automatically using names?
in version 2.46, names are only applied to the routes and methods they are assigned.

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

Successfully merging a pull request may close this issue.

3 participants