Description

I just read the source code tree.go, found the function insertChild creates a redundant tree node, similar to the following path: "/:param/*any"

The actual tree is:

root - path: "/", indices: "", wildChild: true
└─ param - path: ":param", indices: "", wildChild: false
      └─ static - path: "", indices: "/", wildChild: false                       # I think this node is redundant
           └─ catchAll - path: "", indices: "", wildChild: true
                └─ catchAll - path: "/*any", indices: "", wildChild: false

I think the tree should be:

root - path: "/", indices: "", wildChild: true
└─ param - path: ":param", indices: "/", wildChild: false
      └─ catchAll - path: "", indices: "", wildChild: true
            └─ catchAll - path: "/*any", indices: "", wildChild: false

I found the code that caused this problem, tree.go:328:

// if the path doesn't end with the wildcard, then there
// will be another non-wildcard subpath starting with '/'
if len(wildcard) < len(path) {
    path = path[len(wildcard):]

    child := &node{
        priority: 1,
        fullPath: fullPath,
    }
    n.addChild(child)
    n = child
    continue
}

How to reproduce

tree := &node{} tree.addRoute("/:param/*any", nil)

Environment

  • go version: go1.17.5
  • gin version (or commit ref): v2.28.0
  • operating system: windows 10