gin icon indicating copy to clipboard operation
gin copied to clipboard

RedirectTrailingSlash not working if there are more than one child route

Open ashishraman opened this issue 4 years ago • 1 comments

Description

RedirectTrailingSlash not working if there are more than two child route.

How to reproduce

package main

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

func main() {
	g := gin.New()
        router := g.Group("/v1")
	router.GET("/hello", dummyFunction)
        router.GET("/hello/a", dummyFunction)
        router.GET("/hello/b", dummyFunction)
	g.Run(":9000")
}

Expectations

$ curl http://localhost:8201/hello/ should redirect [301] to {{URL}}/hello 

Actual result

Getting 404

The same works if there is only one child /hello/a

Environment

  • go version: go version go1.13.3 linux/amd64
  • gin version (or commit ref): v1.5.0
  • operating system: ubuntu 16.04

ashishraman avatar Jan 16 '20 06:01 ashishraman

Here's what I think is happening.

/hello/ is returning a 404 because the router knows there are routes that live under /hello/

I guessing the fix would be:

if there are routes under /hello/ and you have a trailing slash, and theres nothing after the trailing slash, redirect.

densone avatar Jan 18 '20 13:01 densone