Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind with query param fails with uuid.UUID and uuid.NullUUID #3060

Open
nagzso opened this issue Feb 21, 2022 · 4 comments
Open

Bind with query param fails with uuid.UUID and uuid.NullUUID #3060

nagzso opened this issue Feb 21, 2022 · 4 comments

Comments

@nagzso
Copy link

nagzso commented Feb 21, 2022

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)
@nagzso
Copy link
Author

nagzso commented Feb 21, 2022

Duplicate of #2423

Just found it.

@Bisstocuz
Copy link
Contributor

Bisstocuz commented Feb 23, 2022

Duplicate of #3036

The ID's data type should be string.

@kszafran
Copy link
Contributor

It would probably be fixed by #3045

@eloyekunle
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants