Repository: gin-gonic/gin
Pull request #2983 (2021-12-09 03:12:07)

fix: wrong when wildcard follows named param
# Description
1. set route rule  "/vendor/:x/*y"
2. request via url "/vendor/x/", Gin returns 200. This is right.
3. request via url  "/vendor/x", Gin returns 404.  Instead, it should redirect "/vendor/x" to match the rule "vendor/:x/*y"


# References
This bug is fixed by the [httprouter fix](https://github.com/julienschmidt/httprouter/commit/fe77dd05ab5a80f54110cccf1b7d8681c2648323#diff-050830ac4334170e8d335e1b95519052a3461c5b57a4ca38a0531e35a8f3d388)

@thinkerou please help review this PR. Thanks.


codecov[bot]: # [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) Report
> Merging [#2983](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) (94584bb) into [master](https://codecov.io/gh/gin-gonic/gin/commit/3973c77263d74d5feb2a5923f2c0cbef866b3cc6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) (3973c77) will **decrease** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/gin-gonic/gin/pull/2983/graphs/tree.svg?width=650&height=150&src=pr&token=Qpqxhwery8&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic)](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic)

```diff
@@            Coverage Diff             @@
##           master    #2983      +/-   ##
==========================================
- Coverage   98.60%   98.58%   -0.03%     
==========================================
  Files          41       38       -3     
  Lines        3084     3040      -44     
==========================================
- Hits         3041     2997      -44     
  Misses         30       30              
  Partials       13       13              
```

| Flag | Coverage Δ | |
|---|---|---|
| go-1.13 | `?` | |
| go-1.14 | `?` | |
| go-1.15 | `?` | |
| go-1.16 | `98.58% <100.00%> (-0.03%)` | :arrow_down: |
| go-1.17 | `?` | |
| macos-latest | `?` | |
| nomsgpack | `98.58% <100.00%> (ø)` | |
| ubuntu-latest | `98.58% <100.00%> (-0.03%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic) | Coverage Δ | |
|---|---|---|
| [tree.go](https://codecov.io/gh/gin-gonic/gin/pull/2983/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-dHJlZS5nbw==) | `100.00% <100.00%> (ø)` | |
| [render/msgpack.go](https://codecov.io/gh/gin-gonic/gin/pull/2983/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-cmVuZGVyL21zZ3BhY2suZ28=) | | |
| [binding/binding.go](https://codecov.io/gh/gin-gonic/gin/pull/2983/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-YmluZGluZy9iaW5kaW5nLmdv) | | |
| [binding/msgpack.go](https://codecov.io/gh/gin-gonic/gin/pull/2983/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic#diff-YmluZGluZy9tc2dwYWNrLmdv) | | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic). Last update [3973c77...94584bb](https://codecov.io/gh/gin-gonic/gin/pull/2983?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=gin-gonic).

jincheng9: @appleboy @thinkerou Hi, please help review this PR. Thanks~
jincheng9: @appleboy Plz help review this PR. Thanks.

Patch:
diff --git a/tree.go b/tree.go
index 521d3ef..a30d349 100644
--- a/tree.go
+++ b/tree.go
@@ -535,7 +535,7 @@ walk: // Outer loop for walking the tree
 						// No handle found. Check if a handle for this path + a
 						// trailing slash exists for TSR recommendation
 						n = n.children[0]
-						value.tsr = n.path == "/" && n.handlers != nil
+						value.tsr = (n.path == "/" && n.handlers != nil) || (n.path == "" && n.indices == "/")
 					}
 					return
 


Test patch:
diff --git a/tree_test.go b/tree_test.go
index c372339..94c5338 100644
--- a/tree_test.go
+++ b/tree_test.go
@@ -595,6 +595,7 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
 		"/blog/:p",
 		"/posts/:b/:c",
 		"/posts/b/:c/d/",
+		"/vendor/:x/*y",
 	}
 	for _, route := range routes {
 		recv := catchPanic(func() {
@@ -631,6 +632,7 @@ func TestTreeTrailingSlashRedirect(t *testing.T) {
 		"/api/world/abc/",
 		"/blog/pp/",
 		"/posts/b/c/d",
+		"/vendor/x",
 	}
 
 	for _, route := range tsrRoutes {