-
Notifications
You must be signed in to change notification settings - Fork 8.5k
binding:fix empty value error #2169
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
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2169 +/- ##
==========================================
- Coverage 99.21% 98.68% -0.53%
==========================================
Files 42 44 +2
Lines 3182 2976 -206
==========================================
- Hits 3157 2937 -220
- Misses 17 26 +9
- Partials 8 13 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@vkd @thinkerou Any feedbacks? |
|
|
||
| // ok | ||
| tests := []bindTestData{ | ||
| {need: &needFixUnixNanoEmpty{}, got: &needFixUnixNanoEmpty{}, in: formSource{"createTime": []string{" "}}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The non-empty string does not look to me as a default empty value. For me it looks like a wrong value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The strconv.ParseInt function in Go will report an error when it encounters data beginning with a space. The function of removing spaces is added so that the function does not report an error.
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.ParseInt(" 5", 10, 0))
fmt.Println(strconv.ParseInt("5", 10, 0))
}
/*
0 strconv.ParseInt: parsing " 5": invalid syntax
5 <nil>
*/There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and from my point of view the binding shouldn't do this.
| } | ||
|
|
||
| func setWithProperType(val string, value reflect.Value, field reflect.StructField) error { | ||
| if value.Kind() != reflect.String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not explained by the PR description.
Please update PR description with explanation what cases have you fixed and why.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is a string type, no spaces are removed, and the user data is not modified here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please document your changes in the PR's description. Just add information about what is changed and why.
|
Vote for this pr dispute. @appleboy @vkd @thinkerou package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.ParseInt(" 5", 10, 0))
fmt.Println(strconv.ParseInt("5", 10, 0))
}
/*
0 strconv.ParseInt: parsing " 5": invalid syntax
5 <nil>
*/strings.TrimSpace + 1 |
|
By my perspective, adding strings.TrimSpace - 1 |
| func setWithProperType(val string, value reflect.Value, field reflect.StructField) error { | ||
| // If it is a string type, no spaces are removed, and the user data is not modified here | ||
| if value.Kind() != reflect.String { | ||
| val = strings.TrimSpace(val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If val is not string type, can call func TrimSpace(s string) string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of val is string, as explained below.
If the client command is the following code
curl -H 'intkey: v1" -H "strkey: v2" url
We define the following structure to parse the http header
type needHeader struct {
IntKey int `header:"intkey"
StrKey string `header:"strkey"
}Such a data structure.
Before setting into the structure, regardless of the int or string required by the structure, it is of type string before being parsed.
|
@thinkerou move to 1.7 milestones? |
OK, thanks! |
|
@guonaihong, please help resolve conflicts |
|
No problem,tonight.
…---Original---
From: "Bo-Yi ***@***.***>
Date: Fri, Nov 28, 2025 10:25 AM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [gin-gonic/gin] binding:fix empty value error (#2169)
appleboy left a comment (gin-gonic/gin#2169)
@guonaihong, please help resolve conflicts
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
Here is the code that can report an error
```go
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"time"
)
type header struct {
Duration time.Duration `header:"duration"`
CreateTime time.Time `header:"createTime" time_format:"unix"`
}
func needFix1() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
h := header{}
err := c.ShouldBindHeader(&h)
if err != nil {
c.JSON(500, fmt.Sprintf("fail:%s\n", err))
return
}
c.JSON(200, h)
})
g.Run(":8081")
}
func needFix2() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
h := header{}
err := c.ShouldBindHeader(&h)
if err != nil {
c.JSON(500, fmt.Sprintf("fail:%s\n", err))
return
}
c.JSON(200, h)
})
g.Run(":8082")
}
func sendNeedFix1() {
// send to needFix1
sendBadData("http://127.0.0.1:8081", "duration")
}
func sendNeedFix2() {
// send to needFix2
sendBadData("http://127.0.0.1:8082", "createTime")
}
func sendBadData(url, key string) {
req, err := http.NewRequest("GET", "http://127.0.0.1:8081", nil)
if err != nil {
fmt.Printf("err:%s\n", err)
return
}
// Only the key and no value can cause an error
req.Header.Add(key, "")
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
io.Copy(os.Stdout, rsp.Body)
rsp.Body.Close()
}
func main() {
go needFix1()
go needFix2()
time.Sleep(time.Second / 1000 * 200) // 200ms
sendNeedFix1()
sendNeedFix2()
}
```
6655232 to
2891214
Compare
- Replace 'interface{}' with 'any' alias in bindTestData struct
- Change assert.NoError to require.NoError in TestMappingTimeUnixNano and TestMappingTimeDuration to fail fast on mapping errors
|
@thinkerou @vkd Please help review the PR. |
|
Conflicts have been resolved @appleboy |
vkd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good to me!
|
@guonaihong Thanks. |
Here is the code that can report an error