gin icon indicating copy to clipboard operation
gin copied to clipboard

gin cannot find the correct route in some cases

Open arturiamu opened this issue 8 months ago • 3 comments

Description

I'm trying to use gin to build a k8s restapi service,but it seems that there is something wrong with gin’s routing function.

Details

I have defined the following two routes to handle namespaced resource requests and none-namespaced resource requests respectively.

	namespacedRouter := router.Group("/:api-version/namespaces")
	{
		// /{api-version}/namespaces/{namespace-name}/{resource-kind}
		namespacedRouter.GET("/:namespace-name/:resource-kind", ApiNamespacedList)

		// /{api-version}//namespaces/{namespace-name}/{resource-kind}/{resource-name}
		namespacedRouter.GET("/:namespace-name/:resource-kind/:resource-name", ApiNamespacedGet)
	}

	naneNamespacedRouter := router.Group("/:api-version")
	{
		// /{api-version}/{resource-kind}
		naneNamespacedRouter.GET("/:resource-kind", ApiNoneNamespacedList)

		// /{api-version}/{resource-kind}/{resource-name}
		naneNamespacedRouter.GET("/:resource-kind/:resource-name", ApiNoneNamespacedGet)
	}

These routes work fine for namespaced resources, but only partially match none-namespaced resources.

Like this:

namespaced-list ok api/v1/namespaces/kube-system/pods

namespaced-get ok api/v1/namespaces/kube-system/pods/etcd-minikube

none-namespaced-list ok api/v1/namespaces api/v1/nodes

none-namespaced-get 404 api/v1/namespaces/default

none-namespaced-get ok api/v1/nodes/minikube

In summary, it fails to correctly match the get case for none-namespaced resources named namespaces

My guess is that the namespaces in the path make gin confused and unable to work properly.

Environment

  • go version: golang 1.20
  • gin version (or commit ref): v1.9.1
  • operating system: macOS 13.4 (22F66)

arturiamu avatar Sep 15 '23 03:09 arturiamu