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

[Bug]mixed param and non-param paths #2786

Closed
waising opened this issue Jul 12, 2021 · 6 comments · Fixed by #2796
Closed

[Bug]mixed param and non-param paths #2786

waising opened this issue Jul 12, 2021 · 6 comments · Fixed by #2796
Labels
Milestone

Comments

@waising
Copy link

waising commented Jul 12, 2021

gin version 1.7.2

"/get/abc" = value1
"/get/:param" = value2

GET /get/abc => value1
GET /get/a => value2
GET /get/xyz => value2
GET /get/abcd => 404 (should be value2)

@zihengCat
Copy link
Contributor

zihengCat commented Jul 12, 2021

@waising
Known issue for wildcard router conflicts. You should avoid writing router rules like this.
Try the following:

router.GET("/api/v1/:param", func(c *gin.Context) {
    if c.Param("param") == "abc" {
        // do something for typical param
    }
    // normal case ...
})

@qm012
Copy link
Contributor

qm012 commented Jul 14, 2021

same mistake as #2762

@kszafran
Copy link
Contributor

I wouldn't say it's a "mistake". Isn't this exactly the problem that was supposed to be fixed by the new router?

@appleboy
Copy link
Member

@qm012 I can reproduce the issue with the latest commit d4ca9a0

package main

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

func handler01(c *gin.Context) {
	fmt.Println(c.Params)
	c.String(200, "ok01")
}

func handler02(c *gin.Context) {
	fmt.Println(c.Params)
	c.String(200, "ok02")
}

func main() {
	g := gin.Default()
	g.GET("/get/abc", handler01)
	g.GET("/get/:param", handler02)
	g.Run(":9090")
}

result:

/abc  200 ok02
/a    404
/ab   200 ok02
/xyz  200 ok02
/abcd 200 ok02

@appleboy appleboy mentioned this issue Jul 23, 2021
@qm012
Copy link
Contributor

qm012 commented Jul 23, 2021

@appleboy Please give me some time. I am trying to solve this problem. There is no problem with the first-level route match, but if the multi-level route is greater than or less than the current path, it will not match the original one

@appleboy appleboy added the bug label Jul 23, 2021
@appleboy appleboy added this to the v1.8 milestone Jul 23, 2021
@appleboy
Copy link
Member

@qm012 Thanks.

@waising @zihengCat We move to #2796 for discussion in the fueture.

@appleboy appleboy linked a pull request Jul 24, 2021 that will close this issue
thinkerou pushed a commit that referenced this issue Jul 26, 2021
* update match rule

* add comments
appleboy pushed a commit that referenced this issue Aug 15, 2021
* update match rule

* add comments
Bisstocuz pushed a commit to Bisstocuz/gin that referenced this issue Nov 22, 2021
* update match rule

* add comments

(cherry picked from commit 0a55865)
Bisstocuz pushed a commit to Bisstocuz/gin that referenced this issue Nov 22, 2021
* update match rule

* add comments

(cherry picked from commit 0a55865)
thinkerou pushed a commit that referenced this issue Nov 23, 2021
* update match rule

* add comments
daheige pushed a commit to daheige/gin that referenced this issue Apr 18, 2022
* update match rule

* add comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants