From db9174ae0c2587fe1c755def0f88cb9aba9e9641 Mon Sep 17 00:00:00 2001 From: Dmitry Kutakov Date: Fri, 1 Nov 2019 03:47:40 +0100 Subject: [PATCH] fix ignore walking on form mapping (#1942) (#1943) --- binding/form_mapping.go | 7 ++++--- binding/form_mapping_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index ec78bfee..d6199c4f 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -51,6 +51,10 @@ func mappingByPtr(ptr interface{}, setter setter, tag string) error { } func mapping(value reflect.Value, field reflect.StructField, setter setter, tag string) (bool, error) { + if field.Tag.Get(tag) == "-" { // just ignoring this field + return false, nil + } + var vKind = value.Kind() if vKind == reflect.Ptr { @@ -112,9 +116,6 @@ func tryToSetValue(value reflect.Value, field reflect.StructField, setter setter tagValue = field.Tag.Get(tag) tagValue, opts := head(tagValue, ",") - if tagValue == "-" { // just ignoring this field - return false, nil - } if tagValue == "" { // default value is FieldName tagValue = field.Name } diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index c9d6111b..2a560371 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -269,3 +269,13 @@ func TestMappingMapField(t *testing.T) { assert.NoError(t, err) assert.Equal(t, map[string]int{"one": 1}, s.M) } + +func TestMappingIgnoredCircularRef(t *testing.T) { + type S struct { + S *S `form:"-"` + } + var s S + + err := mappingByPtr(&s, formSource{}, "form") + assert.NoError(t, err) +}