1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-31 22:05:06 +02:00

explicitly return an error instead of hiding it

This commit is contained in:
lipengwei 2021-04-29 09:22:01 +08:00 committed by Martti T
parent 18d7fe11df
commit 1aef300cf4
2 changed files with 4 additions and 6 deletions

View File

@ -145,7 +145,7 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
typeField := typ.Field(i) typeField := typ.Field(i)
structField := val.Field(i) structField := val.Field(i)
if typeField.Anonymous { if typeField.Anonymous {
for structField.Kind() == reflect.Ptr { if structField.Kind() == reflect.Ptr {
structField = structField.Elem() structField = structField.Elem()
} }
} }
@ -155,8 +155,8 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
structFieldKind := structField.Kind() structFieldKind := structField.Kind()
inputFieldName := typeField.Tag.Get(tag) inputFieldName := typeField.Tag.Get(tag)
if typeField.Anonymous && structField.Kind() == reflect.Struct && inputFieldName != "" { if typeField.Anonymous && structField.Kind() == reflect.Struct && inputFieldName != "" {
// if anonymous struct, ignore custom tag // if anonymous struct with query/param/form tags, report an error
inputFieldName = "" return errors.New("query/param/form tags are not allowed with anonymous struct field")
} }
if inputFieldName == "" { if inputFieldName == "" {

View File

@ -370,9 +370,7 @@ func TestBindUnmarshalParamAnonymousFieldPtrCustomTag(t *testing.T) {
*Bar `json:"bar" query:"bar"` *Bar `json:"bar" query:"bar"`
}{&Bar{}} }{&Bar{}}
err := c.Bind(&result) err := c.Bind(&result)
if assert.NoError(t, err) { assert.Contains(t, err.Error(), "query/param/form tags are not allowed with anonymous struct field")
assert.Equal(t, 1, result.Baz)
}
} }
func TestBindUnmarshalTextPtr(t *testing.T) { func TestBindUnmarshalTextPtr(t *testing.T) {