gin icon indicating copy to clipboard operation
gin copied to clipboard

Path conflict issue

Open sreejithag opened this issue 1 year ago • 2 comments

Description

I need two paths in one of my project

/:token /:customPath/:token

i understand the issues with the http router is their any any workaround to fix this issue other than adding a wild card pattern and match with interceptor

How to reproduce

package main

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

func main() {
	r := gin.Default()

	r.GET("/:custom/:token", func(c *gin.Context) {
		custom := c.Param("custom")
		token := c.Param("token")
		c.JSON(200, gin.H{
			"message": "Specific route",
			"custom":  custom,
			"token":   token,
		})
	})

	r.GET("/:token", func(c *gin.Context) {
		token := c.Param("token")
		c.JSON(200, gin.H{
			"message": "General route",
			"token":   token,
		})
	})

	r.Run(":8080") // Start the server on port 8080
}

Expectations

panic: ':token' in new path '/:token' conflicts with existing wildcard ':custom' in existing prefix '/:custom'

Environment

  • go version: 1.23.4
  • gin version (or commit ref): 1.10.0
  • operating system: macOS 15.2

sreejithag avatar Dec 26 '24 13:12 sreejithag

Hello @sreejithag, depending on your project's semantics, you could use a static prefix to work around this issue:

Example 1
Add a static /general/ prefix to mark the general route.

r.GET("/general/:token", ...)
r.GET("/:custom/:token", ...)

Example 2 Make the route for customs explicit by adding a static /customs/ prefix.

r.GET("/customs/:custom/:token", ...)
r.GET("/:token", ...)

pscheid92 avatar Jan 11 '25 09:01 pscheid92

Are there any other ways? I need two routes '/cap/projects/:projectName/environments/:name' '/cap/projects/:name'

CodeFancier avatar Aug 06 '25 11:08 CodeFancier