mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-22 02:43:36 +08:00
fix(binding): preserve whitespace in pointer string collections
This commit is contained in:
parent
0f09c3a9b4
commit
91508b6fe0
@ -328,8 +328,8 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel
|
||||
return err
|
||||
}
|
||||
|
||||
// If it is a string type, no spaces are removed, and the user data is not modified here
|
||||
if value.Kind() != reflect.String {
|
||||
// Preserve string whitespace; defer pointers until their element type is known.
|
||||
if value.Kind() != reflect.String && value.Kind() != reflect.Ptr {
|
||||
val = strings.TrimSpace(val)
|
||||
}
|
||||
|
||||
|
||||
@ -23,6 +23,9 @@ func TestMappingBaseTypes(t *testing.T) {
|
||||
intPtr := func(i int) *int {
|
||||
return &i
|
||||
}
|
||||
text := " \ttest\n "
|
||||
textPtr := &text
|
||||
whitespace := " \t\n "
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
value any
|
||||
@ -45,6 +48,14 @@ func TestMappingBaseTypes(t *testing.T) {
|
||||
{"base type", struct{ F string }{}, "test", string("test")},
|
||||
{"base type", struct{ F *int }{}, "9", intPtr(9)},
|
||||
|
||||
// Preserve string whitespace through collection element pointers.
|
||||
{"whitespace", struct{ F []*string }{}, text, []*string{&text}},
|
||||
{"whitespace", struct{ F [1]*string }{}, text, [1]*string{&text}},
|
||||
{"whitespace", struct{ F []**string }{}, text, []**string{&textPtr}},
|
||||
{"whitespace only", struct{ F []*string }{}, whitespace, []*string{&whitespace}},
|
||||
{"whitespace only", struct{ F [1]*string }{}, whitespace, [1]*string{&whitespace}},
|
||||
{"whitespace", struct{ F []*int }{}, " \t9\n ", []*int{intPtr(9)}},
|
||||
|
||||
// zero values
|
||||
{"zero value", struct{ F int }{}, "", int(0)},
|
||||
{"zero value", struct{ F uint }{}, "", uint(0)},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user