Repository: gin-gonic/gin
Pull request #2366 (2020-05-09 07:45:33)

fix(tree): reassign fullpath when register new node which the same current node
## Proposed changes

While using func `context.FullPath()`, I encountered an issue that it always returns previous registered parent path. It's fine when I change the registration order.

Example: 

```go
func (s *testSuite) registerTest() {
	s.router.GET("/user/:id/status", s.processWithStatus())
	s.router.GET("/user/:id", s.processWithID())
}

func (s *testSuite) processWithStatus() gin.HandlerFunc {
	return func(c *gin.Context) {
		logger.Infof("XXX - FullPath /user/:id/status ---> %s", c.FullPath())
		return
	}
}

func (s *testSuite) processWithID() gin.HandlerFunc {
	return func(c *gin.Context) {
		logger.Infof("XXX - FullPath /user/:id ---> %s", c.FullPath())
		return
	}
}
```

Result: 
```
XXX - FullPath /user/:id/status ---> /service/api/v1/user/:id/status
XXX - FullPath /user/:id ---> /service/api/v1/user/:id/status
```

But, when I register with different the order of router, such as: 

```go
func (s *testSuite) registerTest() {
	s.router.GET("/user/:id", s.processWithID())
	s.router.GET("/user/:id/status", s.processWithStatus())
}
```

It's every thing so ok. 

I have fix this bug and updated the test case. 
Hope you spend your time review and accept my pull request with this issue.

## Types of changes

- [x] Bugfix (non-breaking change which fixes an issue)

## Checklist

- [x] Lint and unit tests pass locally with my changes


codecov[bot]: # [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=h1) Report
> Merging [#2366](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=desc) into [master](https://codecov.io/gh/gin-gonic/gin/commit/82b284fd774d6bf68c934a3f335b816c61edf66f&el=desc) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/gin-gonic/gin/pull/2366/graphs/tree.svg?width=650&height=150&src=pr&token=Qpqxhwery8)](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master    #2366   +/-   ##
=======================================
  Coverage   98.48%   98.48%           
=======================================
  Files          41       41           
  Lines        2305     2310    +5     
=======================================
+ Hits         2270     2275    +5     
  Misses         20       20           
  Partials       15       15           
```


| [Impacted Files](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tree.go](https://codecov.io/gh/gin-gonic/gin/pull/2366/diff?src=pr&el=tree#diff-dHJlZS5nbw==) | `99.14% <100.00%> (+<0.01%)` | :arrow_up: |
| [render/text.go](https://codecov.io/gh/gin-gonic/gin/pull/2366/diff?src=pr&el=tree#diff-cmVuZGVyL3RleHQuZ28=) | `100.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=footer). Last update [82b284f...c2e0974](https://codecov.io/gh/gin-gonic/gin/pull/2366?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

vinhha96: Hi @appleboy , @thinkerou. Please help me review this pull request. 
Many thanks.

Patch:
diff --git a/tree.go b/tree.go
index e3aa919..f528fa3 100644
--- a/tree.go
+++ b/tree.go
@@ -240,6 +240,7 @@ walk:
 			panic("handlers are already registered for path '" + fullPath + "'")
 		}
 		n.handlers = handlers
+		n.fullPath = fullPath
 		return
 	}
 }


Test patch:
diff --git a/routes_test.go b/routes_test.go
index 360ade6..11ff71a 100644
--- a/routes_test.go
+++ b/routes_test.go
@@ -593,6 +593,9 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
 		"/simple-two/one-two",
 		"/project/:name/build/*params",
 		"/project/:name/bui",
+		"/user/:id/status",
+		"/user/:id",
+		"/user/:id/profile",
 	}
 
 	for _, route := range routes {