Description

QueryParam binding with property of uuid.UUID or uuid.NullUUID (Google) type in a struct doesn't work. Using these types in body works as expected.

How to reproduce

package main

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

func main() {
    r := gin.Default()
    r.GET("/api/test/query-params/uuid", uuidQueryParamHandler)
    r.GET("/api/test/query-params/null-uuid", nullUUIDQueryParamHandler)
    r.Run(fmt.Sprintf("localhost:%d", 8095))
}

func uuidQueryParamHandler(c *gin.Context) {
    var model struct {
        ID uuid.UUID `json:"id" form:"id" binding:"uuid"`
    }

    err := c.ShouldBind(&model)

    if err != nil {
        fmt.Printf("Failed to bind query param, err: %+v\n", err)
        c.AbortWithError(http.StatusBadRequest, err)
        return
    }

    c.JSON(http.StatusOK, model)
}

func nullUUIDQueryParamHandler(c *gin.Context) {
    var model struct {
        ID uuid.NullUUID `json:"id" form:"id" binding:"uuid"`
    }

    err := c.ShouldBind(&model)

    if err != nil {
        fmt.Printf("Failed to bind query param, err: %+v\n", err)
        c.AbortWithError(http.StatusBadRequest, err)
        return
    }

    c.JSON(http.StatusOK, model)
}

Expectations

uuid.UUID

$ curl -X GET -i http://localhost:8095/api/test/query-params/uuid?id=9c018e73-449d-4a19-93bb-3a6b5ef22363
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Mon, 21 Feb 2022 11:05:58 GMT
Content-Length: 45

{"id":"9c018e73-449d-4a19-93bb-3a6b5ef22363"}

uuid.NullUUID

$ curl -X GET -i http://localhost:8095/api/test/query-params/null-uuid?id=9c018e73-449d-4a19-93bb-3a6b5ef22363
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Mon, 21 Feb 2022 11:05:58 GMT
Content-Length: 45

{"id":"9c018e73-449d-4a19-93bb-3a6b5ef22363"}

Actual result

uuid.UUID

$ curl -X GET -i http://localhost:8095/api/test/query-params/uuid?id=9c018e73-449d-4a19-93bb-3a6b5ef22363
HTTP/1.1 400 Bad Request
Date: Mon, 21 Feb 2022 10:57:46 GMT
Content-Length: 0

Console log:

Error #01: ["9c018e73-449d-4a19-93bb-3a6b5ef22363"] is not valid value for uuid.UUID

uuid.NullUUID

$ curl -X GET -i http://localhost:8095/api/test/query-params/null-uuid?id=9c018e73-449d-4a19-93bb-3a6b5ef22363
HTTP/1.1 400 Bad Request
Date: Mon, 21 Feb 2022 11:03:02 GMT
Content-Length: 0

Console log:

Error #01: invalid character 'c' after top-level value

Environment

  • go version: 1.17
  • gin version (or commit ref): v1.7.7
  • operating system: Windows 10 Pro (19044.1466)

Comment From: nagzso

Duplicate of #2423

Just found it.

Comment From: Bisstocuz

Duplicate of #3036

The ID's data type should be string.

Comment From: kszafran

It would probably be fixed by https://github.com/gin-gonic/gin/pull/3045

Comment From: eloyekunle

This is still an issue. When will it be fixed in gin?

Comment From: n-danvers

This appears to still be an issue