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

Allow different param names in different methods with same path scheme #2209

Merged
merged 5 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ type (
routes map[string]*Route
echo *Echo
}
methodContext struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that context describes this struct correctly. In world of http servers and Echo context is something more temporary. I have used routeMethod in v5 for similar struct but in v5 methodHandler struct is also renamed to routeMethods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to routeMethod

handler HandlerFunc
}
node struct {
kind kind
label byte
Expand All @@ -32,17 +35,17 @@ type (
kind uint8
children []*node
methodHandler struct {
connect HandlerFunc
delete HandlerFunc
get HandlerFunc
head HandlerFunc
options HandlerFunc
patch HandlerFunc
post HandlerFunc
propfind HandlerFunc
put HandlerFunc
trace HandlerFunc
report HandlerFunc
connect *methodContext
delete *methodContext
get *methodContext
head *methodContext
options *methodContext
patch *methodContext
post *methodContext
propfind *methodContext
put *methodContext
trace *methodContext
report *methodContext
allowHeader string
}
)
Expand Down Expand Up @@ -209,7 +212,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
currentNode.prefix = search
if h != nil {
currentNode.kind = t
currentNode.addHandler(method, h)
currentNode.addHandler(method, &methodContext{
handler: h,
})
currentNode.ppath = ppath
currentNode.pnames = pnames
}
Expand Down Expand Up @@ -257,13 +262,17 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
if lcpLen == searchLen {
// At parent node
currentNode.kind = t
currentNode.addHandler(method, h)
currentNode.addHandler(method, &methodContext{
handler: h,
})
currentNode.ppath = ppath
currentNode.pnames = pnames
} else {
// Create child node
n = newNode(t, search[lcpLen:], currentNode, nil, new(methodHandler), ppath, pnames, nil, nil)
n.addHandler(method, h)
n.addHandler(method, &methodContext{
handler: h,
})
// Only Static children could reach here
currentNode.addStaticChild(n)
}
Expand All @@ -278,7 +287,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
}
// Create child node
n := newNode(t, search, currentNode, nil, new(methodHandler), ppath, pnames, nil, nil)
n.addHandler(method, h)
n.addHandler(method, &methodContext{
handler: h,
})
switch t {
case staticKind:
currentNode.addStaticChild(n)
Expand All @@ -291,7 +302,9 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
} else {
// Node already exists
if h != nil {
currentNode.addHandler(method, h)
currentNode.addHandler(method, &methodContext{
handler: h,
})
currentNode.ppath = ppath
if len(currentNode.pnames) == 0 { // Issue #729
currentNode.pnames = pnames
Expand Down Expand Up @@ -345,7 +358,12 @@ func (n *node) findChildWithLabel(l byte) *node {
return nil
}

func (n *node) addHandler(method string, h HandlerFunc) {
func (n *node) addHandler(method string, h *methodContext) {
if h.handler == nil {
n.isHandler = n.methodHandler.isHandler()
return
}

switch method {
case http.MethodConnect:
n.methodHandler.connect = h
Expand All @@ -372,14 +390,10 @@ func (n *node) addHandler(method string, h HandlerFunc) {
}

n.methodHandler.updateAllowHeader()
if h != nil {
n.isHandler = true
} else {
n.isHandler = n.methodHandler.isHandler()
}
n.isHandler = true
}

func (n *node) findHandler(method string) HandlerFunc {
func (n *node) findHandler(method string) *methodContext {
switch method {
case http.MethodConnect:
return n.methodHandler.connect
Expand Down Expand Up @@ -530,7 +544,7 @@ func (r *Router) Find(method, path string, c Context) {
previousBestMatchNode = currentNode
}
if h := currentNode.findHandler(method); h != nil {
matchedHandler = h
matchedHandler = h.handler
break
}
}
Expand Down Expand Up @@ -581,7 +595,7 @@ func (r *Router) Find(method, path string, c Context) {
previousBestMatchNode = currentNode
}
if h := currentNode.findHandler(method); h != nil {
matchedHandler = h
matchedHandler = h.handler
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2380,7 +2380,7 @@ func TestRouterHandleMethodOptions(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, tc.expectStatus, rec.Code)
}
assert.Equal(t, tc.expectAllowHeader, c.Response().Header().Get("Allow"))
assert.Equal(t, tc.expectAllowHeader, c.Response().Header().Get(HeaderAllow))
})
}
}
Expand Down