Skip to content

Parameter name affects route registration ordering #1052

Closed
@filet0fish

Description

@filet0fish

Issue Description

Routes with identical parameter names can return the wrong value if not registered in a specific order.

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

Routing documentation states "Routes can be written in any order."

Actual behaviour

Route order does matter in this edge case. If registered in the wrong order, the parameter names resolve to the wrong segment of the url pattern.

Steps to reproduce

Register a set of route patterns:

e.GET("/:a/:b/:c/:id", func(c echo.Context) error {
	return c.HTML(500, "FAILS :id returns value of :c")
})
e.GET("/:a/:id", func(c echo.Context) error {
	return c.HTML(200, "WORKS")
})
e.GET("/:a/:e/:id", func(c echo.Context) error {
	return c.HTML(200, "WORKS")
})

If the first route is moved to last position, all will return the correct value for c.Param("id"). If any one of the 3 routes is removed, the other two will return the correct value.

Working code to debug

package main

import "github.com/labstack/echo"

func main() {
	e := echo.New()

	e.GET("/:a/:b/:c/:id", func(c echo.Context) error {
		return c.HTML(500, "FAILS :id returns :c, ID == "+c.Param("id"))
	})
	e.GET("/:a/:id", func(c echo.Context) error {
		return c.HTML(200, "WORKS, ID == "+c.Param("id"))
	})
	e.GET("/:a/:e/:id", func(c echo.Context) error {
		return c.HTML(200, "WORKS, ID == "+c.Param("id"))
	})

	e.Logger.Fatal(e.Start(":8888"))
}

http://localhost:8888/foo/bar/baz/123 returns FAILS :id returns :c, ID == baz

Version/commit

version: ^3.1.0
version: b338075

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions