Skip to content

Commit 1aef300

Browse files
lipengweialdas
authored andcommitted
explicitly return an error instead of hiding it
1 parent 18d7fe1 commit 1aef300

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

bind.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
145145
typeField := typ.Field(i)
146146
structField := val.Field(i)
147147
if typeField.Anonymous {
148-
for structField.Kind() == reflect.Ptr {
148+
if structField.Kind() == reflect.Ptr {
149149
structField = structField.Elem()
150150
}
151151
}
@@ -155,8 +155,8 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
155155
structFieldKind := structField.Kind()
156156
inputFieldName := typeField.Tag.Get(tag)
157157
if typeField.Anonymous && structField.Kind() == reflect.Struct && inputFieldName != "" {
158-
// if anonymous struct, ignore custom tag
159-
inputFieldName = ""
158+
// if anonymous struct with query/param/form tags, report an error
159+
return errors.New("query/param/form tags are not allowed with anonymous struct field")
160160
}
161161

162162
if inputFieldName == "" {

bind_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ func TestBindUnmarshalParamAnonymousFieldPtrCustomTag(t *testing.T) {
370370
*Bar `json:"bar" query:"bar"`
371371
}{&Bar{}}
372372
err := c.Bind(&result)
373-
if assert.NoError(t, err) {
374-
assert.Equal(t, 1, result.Baz)
375-
}
373+
assert.Contains(t, err.Error(), "query/param/form tags are not allowed with anonymous struct field")
376374
}
377375

378376
func TestBindUnmarshalTextPtr(t *testing.T) {

0 commit comments

Comments
 (0)