Skip to content
Myr

fix: Add pointer support for url query params (#3659) (#3666)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The pointer support in url query params (using []*Struct for binding query params) was previously available in Gin, but was removed in commit 0d50ce8 since there wasn't a test case for such a scenario, and so the case block was removed as a redundant one.

authored by Omkar P · committed by GitHub · parent 49f45a5 · Browse files · 2 files+38 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−0 removed

  • other:+38 added−0 removed

2 files+38 added−0 removed

modified:binding/form_mapping_test.go+33 added
267 unmodified lines
268 assert.NoError(t, err)269 assert.Equal(t, 9, s.J.I)270}271272func TestMappingPtrField(t *testing.T) {273 type ptrStruct struct {274 Key int64 `json:"key"`275 }276277 type ptrRequest struct {278 Items []*ptrStruct `json:"items" form:"items"`279 }280281 var err error282283 // With 0 items.284 var req0 ptrRequest285 err = mappingByPtr(&req0, formSource{}, "form")286 assert.NoError(t, err)287 assert.Empty(t, req0.Items)288289 // With 1 item.290 var req1 ptrRequest291 err = mappingByPtr(&req1, formSource{"items": {`{"key": 1}`}}, "form")292 assert.NoError(t, err)293 assert.Len(t, req1.Items, 1)294 assert.EqualValues(t, 1, req1.Items[0].Key)295296 // With 2 items.297 var req2 ptrRequest298 err = mappingByPtr(&req2, formSource{"items": {`{"key": 1}`, `{"key": 2}`}}, "form")299 assert.NoError(t, err)300 assert.Len(t, req2.Items, 2)301 assert.EqualValues(t, 1, req2.Items[0].Key)302 assert.EqualValues(t, 2, req2.Items[1].Key)303}304305func TestMappingMapField(t *testing.T) {306 var s struct {307 M map[string]int308 }
15 unmodified lines
modified:binding/form_mapping.go+5 added
237 unmodified lines
238 }239 return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())240 case reflect.Map:241 return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface())242 case reflect.Ptr:243 if !value.Elem().IsValid() {244 value.Set(reflect.New(value.Type().Elem()))245 }246 return setWithProperType(val, value.Elem(), field)247 default:248 return errUnknownType249 }250 return nil
158 unmodified lines