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 default value example can't run the correct result. #4138

Closed
DachiChang opened this issue Jan 15, 2025 · 2 comments
Closed

Bind default value example can't run the correct result. #4138

DachiChang opened this issue Jan 15, 2025 · 2 comments

Comments

@DachiChang
Copy link

  • With issues:
    • Use the search tool before opening a new issue.
    • Please provide source code and commit sha if you found a bug.
    • Review existing issues and provide feedback or react to them.

Description

From the document example https://github.com/gin-gonic/gin/blob/master/docs/doc.md#bind-default-value-if-none-provided, can't run the correct result which is same as example.

How to reproduce

package main

import (
	"net/http"

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

type Person struct {
	Name      string    `form:"name,default=William"`
	Age       int       `form:"age,default=10"`
	Friends   []string  `form:"friends,default=Will;Bill"`
	Addresses [2]string `form:"addresses,default=foo bar" collection_format:"ssv"`
	LapTimes  []int     `form:"lap_times,default=1;2;3" collection_format:"csv"`
}

func main() {
	g := gin.Default()
	g.POST("/person", func(c *gin.Context) {
		var req Person
		if err := c.ShouldBindQuery(&req); err != nil {
			c.JSON(http.StatusBadRequest, err)
			return
		}
		c.JSON(http.StatusOK, req)
	})
	_ = g.Run("localhost:8080")
}

Expectations

$ curl -X POST http://localhost:8080/person

{"Name":"William","Age":10,"Friends":["Will","Bill"],"Colors":["red","blue"],"LapTimes":[1,2,3]}

Actual result

$ curl -X POST http://localhost:8080/person

{}

Environment

  • go version: 1.23.2
  • gin version (or commit ref): v1.10.0
  • operating system: mac os 14.5
@kingcanfish
Copy link
Contributor

it is new feature after the gin version:v1.10.0, detail see #3986
if you want use it , maybe you need
go get github.com/gin-gonic/gin@master use the master branch

@DachiChang
Copy link
Author

thx, I can get the right result when I upgraded to the master branch of 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

2 participants