Skip to content

Route conflict #875

Description

@mmindenhall

Description

If I have two routes defined, one with a static value and one with a path param, the static value cannot be used as the path param even if the two routes do not have the same number of path segments.

For example, if I define these two routes:

GET /users/:id/comments   // 3 path segments
GET /:resourceName/:id    // 2 path segments

The second route fails when :resourceId is users (but works for any other value). I'm confused by this behavior. I know the matching order is static -> param -> any, but I would have thought that if no match was found down the static /users path, the router would then attempt to match down the /:resourceId path.

Checklist

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

Expected behaviour

All of these requests should work:

GET /users/1/comments
GET /boozers/1
GET /users/1

Actual behaviour

These two work:

GET /users/1/comments
GET /boozers/1

But this fails:

GET /users/1

Steps to reproduce

Working code to debug

package main

import (
	"testing"

	"github.com/labstack/echo"
	"github.com/stretchr/testify/assert"
)

func TestRouteConflict(t *testing.T) {
	e := echo.New()
	r := e.Router()

	e.GET("/users/:id/comments", func(c echo.Context) error {
		c.Set("r", 1)
		return nil
	})

	e.GET("/:resourceName/:id", func(c echo.Context) error {
		c.Set("r", 2)
		return nil
	})

	c := e.NewContext(nil, nil)
	r.Find(echo.GET, "/users/1/comments", c)
	c.Handler()(c)
	assert.Equal(t, 1, c.Get("r"))

	c = e.NewContext(nil, nil)
	r.Find(echo.GET, "/boozers/1", c)
	c.Handler()(c)
	assert.Equal(t, 2, c.Get("r"))

	c = e.NewContext(nil, nil)
	r.Find(echo.GET, "/users/1", c)
	c.Handler()(c)
	assert.Equal(t, 2, c.Get("r")) // this assertion fails
}

Version/commit

3.1.0-rc1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions