fix(binding): leave pointer fields nil for empty form values

BindQuery (and other form bindings) turned a present but empty value
like "age=" into a pointer to the zero value, so callers could not tell
it apart from "age=0". An absent key already left the pointer nil.

setByForm now reports an empty value with no default as unset for
numeric and boolean fields. Plain fields keep their zero value either
way, strings keep their existing behavior, and time.Duration keeps its
existing handling. This also covers form and URI bindings, which share
the same mapping code.

Fixes #3778.
This commit is contained in:
Sanjay Santhanam 2026-11-03 15:09:16 -08:00
parent 0f09c3a9b4
commit 978618d697
2 changed files with 65 additions and 0 deletions

View File

@ -306,6 +306,21 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
default:
var val string
if !ok || len(vs) == 0 || (len(vs) > 0 && vs[0] == "") {
// An empty value with no default is the same as no value for
// numeric and boolean fields. Reporting it as unset keeps a
// pointer field nil instead of pointing at a zero value, so
// callers can tell "age=" apart from "age=0". Plain fields
// are unaffected: they keep their zero value either way.
// time.Duration keeps its existing handling and is left out
// of this change.
if !opt.isDefaultExists && !isDuration(value) {
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.Bool:
return false, nil
}
}
val = opt.defaultValue
} else if len(vs) > 0 {
val = vs[0]
@ -507,6 +522,10 @@ func setSlice(vals []string, value reflect.Value, field reflect.StructField, opt
return nil
}
func isDuration(value reflect.Value) bool {
return value.Type() == reflect.TypeOf(time.Duration(0))
}
func setTimeDuration(val string, value reflect.Value) error {
if val == "" {
val = "0"

View File

@ -173,6 +173,52 @@ func TestMappingFormWithEmptyToDefault(t *testing.T) {
assert.Equal(t, "DefVal", s.F)
}
func TestMappingEmptyValueLeavesPointerFieldsNil(t *testing.T) {
var s struct {
Age *int `form:"age"`
Count *uint `form:"count"`
Score *float64 `form:"score"`
Flag *bool `form:"flag"`
Name *string `form:"name"`
Plain int `form:"plain"`
}
err := mapForm(&s, map[string][]string{
"age": {""},
"count": {""},
"score": {""},
"flag": {""},
"name": {""},
"plain": {""},
})
require.NoError(t, err)
assert.Nil(t, s.Age)
assert.Nil(t, s.Count)
assert.Nil(t, s.Score)
assert.Nil(t, s.Flag)
// Strings keep their previous behavior: a present but empty value
// still produces a pointer to "".
require.NotNil(t, s.Name)
assert.Equal(t, "", *s.Name)
assert.Equal(t, 0, s.Plain)
}
func TestMappingPointerFieldZeroValueStaysSet(t *testing.T) {
var s struct {
Age *int `form:"age"`
}
err := mapForm(&s, map[string][]string{"age": {"0"}})
require.NoError(t, err)
require.NotNil(t, s.Age)
assert.Equal(t, 0, *s.Age)
var missing struct {
Age *int `form:"age"`
}
err = mapForm(&missing, map[string][]string{})
require.NoError(t, err)
assert.Nil(t, missing.Age)
}
func TestMapFormWithTag(t *testing.T) {
var s struct {
F int `externalTag:"field"`