Skip to content

Route params mixup #78

@CaptainCodeman

Description

@CaptainCodeman

First of all, love the project & the philosophy behind it - it's the perfect approach for Go 1.7+ IMO 👍

But I'm having an issue with some supporting some legacy route formats. Requests map to the correct handlers but the parameters don't match up correctly. Here's a simplified example replacing the routes in TestTree to show the issue I'm having:

func TestTree(t *testing.T) {
    hDate := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
    hCat := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

    tr := &node{}

    tr.InsertRoute(mGET, "/items/:year/:month", hDate)
    tr.InsertRoute(mGET, "/items/:category", hCat)

    tests := []struct {
        r string            // input request path
        h http.Handler      // output matched handler
        p map[string]string // output params
    }{
        {r: "/items/2016/08", h: hDate, p: map[string]string{"year": "2016", "month": "08"}},
        {r: "/items/things", h: hCat, p: map[string]string{"category": "things"}},
    }

That results in:

--- FAIL: TestTree (0.00s)
    tree_test.go:59: input [1]: find '/items/things' expecting params:map[category:things] , got:map[year:things]

Note that the /items/:category handler is correctly used but the parameter is called :year instead from the previous route.

It looks like it's due to how the tree is built with the name of the parameter ignored so it then gets the name from the previous node:

2016/08/23 10:40:56 [node 0 parent:0] typ:0 prefix: label: numEdges:1 isLeaf:false
2016/08/23 10:40:56 [node 1 parent:0] typ:0 prefix:/items/ label:/ numEdges:1 isLeaf:false
2016/08/23 10:40:56 [node 2 parent:1] typ:2 prefix::year label:: numEdges:1 isLeaf:true handler:map[512:<nil> 4:0x90450]
2016/08/23 10:40:56 [node 3 parent:2] typ:0 prefix:/ label:/ numEdges:1 isLeaf:false
2016/08/23 10:40:56 [node 4 parent:3] typ:2 prefix::month label:: numEdges:0 isLeaf:true handler:map[512:<nil> 4:0x90440]

Is this simply not supported (I know many Go routers are strict about the route variations, others such as Echo cope better with suffix variations) or is it a bug?

Many thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions