Skip to content

Commit d6bedad

Browse files
authored
Fix parse forms explode object with optional args (#456)
1 parent 271d9df commit d6bedad

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

bindparam_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ func TestBindParamsToExplodedObject(t *testing.T) {
447447
err = bindParamsToExplodedObject("date", values, &nTDstDate)
448448
assert.EqualValues(t, expectedDate, nTDstDate)
449449

450+
type ObjectWithOptional struct {
451+
Time *time.Time `json:"time,omitempty"`
452+
}
453+
454+
var optDstTime ObjectWithOptional
455+
err = bindParamsToExplodedObject("explodedObject", values, &optDstTime)
456+
assert.NoError(t, err)
457+
assert.EqualValues(t, &now, optDstTime.Time)
450458
}
451459

452460
func TestBindStyledParameterWithLocation(t *testing.T) {

bindstring.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ func BindStringToObject(src string, dst interface{}) error {
4141
t = v.Type()
4242
}
4343

44+
// For some optioinal args
45+
if t.Kind() == reflect.Ptr {
46+
if v.IsNil() {
47+
v.Set(reflect.New(t.Elem()))
48+
}
49+
50+
v = reflect.Indirect(v)
51+
t = v.Type()
52+
}
53+
4454
// The resulting type must be settable. reflect will catch issues like
4555
// passing the destination by value.
4656
if !v.CanSet() {

0 commit comments

Comments
 (0)