diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 6982fd4f..1fb5d539 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -140,6 +140,8 @@ type setOptions struct { defaultValue string // parser specifies what interface to use for reading the request & default values (e.g. `encoding.TextUnmarshaler`) parser string + // fieldName is the tag value (or the Go field name when the tag is empty) used to prefix bind errors + fieldName string } func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) { @@ -176,6 +178,7 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter } } + setOpt.fieldName = tagValue return setter.TrySet(value, field, tagValue, setOpt) } @@ -196,6 +199,14 @@ func trySetCustom(val string, value reflect.Value) (isSet bool, err error) { return false, nil } +// prefixBindErr prefixes a bind error with the form/query field name. +func prefixBindErr(fieldName string, err error) error { + if err == nil || fieldName == "" { + return err + } + return fmt.Errorf("%s: %w", fieldName, err) +} + // trySetUsingParser tries to set a custom type value based on the presence of the "parser" tag on the field. // If the parser tag does not exist or does not match any of the supported parsers, gin will skip over this. func trySetUsingParser(val string, value reflect.Value, parser string) (isSet bool, err error) { @@ -247,6 +258,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][ if !ok && !opt.isDefaultExists { return false, nil } + if opt.fieldName == "" { + opt.fieldName = tagValue + } switch value.Kind() { case reflect.Slice: @@ -264,9 +278,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][ } if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok { - return ok, err + return ok, prefixBindErr(opt.fieldName, err) } else if ok, err = trySetCustom(vs[0], value); ok { - return ok, err + return ok, prefixBindErr(opt.fieldName, err) } if vs, err = trySplit(vs, field); err != nil { @@ -289,9 +303,9 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][ } if ok, err = trySetUsingParser(vs[0], value, opt.parser); ok { - return ok, err + return ok, prefixBindErr(opt.fieldName, err) } else if ok, err = trySetCustom(vs[0], value); ok { - return ok, err + return ok, prefixBindErr(opt.fieldName, err) } if vs, err = trySplit(vs, field); err != nil { @@ -312,20 +326,24 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][ } if ok, err = trySetUsingParser(val, value, opt.parser); ok { - return ok, err + return ok, prefixBindErr(opt.fieldName, err) } else if ok, err = trySetCustom(val, value); ok { - return ok, err + return ok, prefixBindErr(opt.fieldName, err) } return true, setWithProperType(val, value, field, opt) } } func setWithProperType(val string, value reflect.Value, field reflect.StructField, opt setOptions) error { + // fieldName is set by tryToSetValue; the Go field name only covers direct calls. + if opt.fieldName == "" { + opt.fieldName = field.Name + } // this if-check is required for parsing nested types like []MyId, where MyId is [12]byte if ok, err := trySetUsingParser(val, value, opt.parser); ok { - return err + return prefixBindErr(opt.fieldName, err) } else if ok, err = trySetCustom(val, value); ok { - return err + return prefixBindErr(opt.fieldName, err) } // If it is a string type, no spaces are removed, and the user data is not modified here diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index c78f7398..3474c9d5 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -505,6 +505,84 @@ func (f *customUnmarshalParamHex) UnmarshalParam(param string) error { return nil } +var errBoom = errors.New("boom") + +type failUnmarshalParam struct{} + +func (f *failUnmarshalParam) UnmarshalParam(param string) error { + return errBoom +} + +type failUnmarshalText string + +func (f *failUnmarshalText) UnmarshalText(text []byte) error { + return errBoom +} + +func TestBindUnmarshalerErrorIncludesFieldName(t *testing.T) { + var formTag struct { + ID failUnmarshalParam `form:"uuid4"` + } + var queryTag struct { + ID failUnmarshalParam `query:"uuid4"` + } + var pointer struct { + ID *failUnmarshalParam `form:"uuid4"` + } + var slice struct { + IDs []failUnmarshalParam `form:"uuid4"` + } + var nested struct { + Inner struct { + ID failUnmarshalParam `form:"uuid4"` + } + } + var noTag struct { + ID failUnmarshalParam + } + var textParser struct { + ID failUnmarshalText `form:"uuid4,parser=encoding.TextUnmarshaler"` + } + + tests := []struct { + name string + ptr any + tag string + want string + }{ + {"form tag", &formTag, "form", "uuid4: boom"}, + {"query tag", &queryTag, "query", "uuid4: boom"}, + {"pointer field", &pointer, "form", "uuid4: boom"}, + {"slice element", &slice, "form", "uuid4: boom"}, + {"nested struct", &nested, "form", "uuid4: boom"}, + {"TextUnmarshaler parser", &textParser, "form", "uuid4: boom"}, + {"no tag falls back to field name", &noTag, "form", "ID: boom"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + form := formSource{"uuid4": {"xxx"}, "ID": {"xxx"}} + err := mappingByPtr(tt.ptr, form, tt.tag) + require.EqualError(t, err, tt.want) + require.ErrorIs(t, err, errBoom) + }) + } +} + +func TestBindUnmarshalerErrorNotPrefixedWithoutParser(t *testing.T) { + // Without a parser tag, UnmarshalText is not used and the value is set as a plain string. + var s struct { + ID failUnmarshalText `form:"uuid4"` + } + require.NoError(t, mappingByPtr(&s, formSource{"uuid4": {"xxx"}}, "form")) + assert.Equal(t, failUnmarshalText("xxx"), s.ID) +} + +func TestPrefixBindErr(t *testing.T) { + require.NoError(t, prefixBindErr("uuid4", nil)) + assert.Equal(t, errBoom, prefixBindErr("", errBoom)) + require.EqualError(t, prefixBindErr("uuid4", errBoom), "uuid4: boom") +} + func TestMappingCustomUnmarshalParamHexWithFormTag(t *testing.T) { var s struct { Foo customUnmarshalParamHex `form:"foo"`