Description

Snipaste_2023-12-14_18-16-11

I am testing the official example regarding the ShouldBindUri method provided by Gin, as shown here: https://gin-gonic.com/docs/examples/bind-uri/. I noticed that even when the URI parameters are correctly bound, the method still returns a non-nil error. Is this a bug? The Gin version I am using is v1.9.1.

reproduce

package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
    "net/http"
)

type Person struct {
    ID   string `uri:"id" binding:"required,uuid"`
    Name string `uri:"name" binding:"required"`
}

func main() {
    r := gin.Default()
    r.GET("/:id/:name", func(c *gin.Context) {
        var person Person
        if err := c.ShouldBindUri(&person); err != nil {
            c.Status(http.StatusBadRequest)
        } else {
            c.JSON(http.StatusOK, gin.H{
                "id":   person.ID,
                "name": person.Name,
            })
        }
    })
    err := r.Run(":9090")
    if err != nil {
        fmt.Printf("gin server start failed, err: %v \n", err)
    }
}

Expectations

$ curl http://localhost:9090/lufy/987fbc97-4bed-5078-9f07-9141ba07c9f3
{"name":  "lufy", "id":  "987fbc97-4bed-5078-9f07-9141ba07c9f3"}

Actual result

$ curl http://localhost:9090/lufy/987fbc97-4bed-5078-9f07-9141ba07c9f3
[GIN] 2023/12/14 - 18:14:34 | 400 |       525.4µs |             ::1 | GET      "/lufy/541dae8c-9a1a-657a-5fc2-5c229a5bb1fa"

Environment

  • go version: 1.21.3
  • gin version (or commit ref): v1.9.1
  • operating system: win10 22h2