diff --git a/tonic/handler.go b/tonic/handler.go index 18ea0a3..c125593 100644 --- a/tonic/handler.go +++ b/tonic/handler.go @@ -209,7 +209,11 @@ func bind(c *gin.Context, v reflect.Value, tag string, extract extractor) error // if no values were returned. def, ok := ft.Tag.Lookup(DefaultTag) if ok && len(fieldValues) == 0 { - fieldValues = append(fieldValues, def) + if c.GetBool(ExplodeTag) { + fieldValues = append(fieldValues, strings.Split(def, ",")...) + } else { + fieldValues = append(fieldValues, def) + } } if len(fieldValues) == 0 { continue diff --git a/tonic/tonic_test.go b/tonic/tonic_test.go index c8d7b5d..30b77e3 100644 --- a/tonic/tonic_test.go +++ b/tonic/tonic_test.go @@ -89,8 +89,11 @@ func TestPathQuery(t *testing.T) { // Explode. tester.AddCall("query-explode", "GET", "/query?param=foo¶m-explode=a¶m-explode=b¶m-explode=c", "").Checkers(iffy.ExpectStatus(200), expectStringArr("param-explode", "a", "b", "c")) - tester.AddCall("query-explode-disabled", "GET", "/query?param=foo¶m-explode-disabled=x,y,z", "").Checkers(iffy.ExpectStatus(200), expectStringArr("param-explode-disabled", "x", "y", "z")) + tester.AddCall("query-explode-disabled-coma", "GET", "/query?param=foo¶m-explode-disabled=x,y,z", "").Checkers(iffy.ExpectStatus(200), expectStringArr("param-explode-disabled", "x", "y", "z")) tester.AddCall("query-explode-disabled", "GET", "/query?param=foo¶m-explode-disabled=a¶m-explode-disabled=b", "").Checkers(iffy.ExpectStatus(400)) + tester.AddCall("query-explode-comma-string", "GET", "/query?param=foo¶m-explode-string=x,y,z", "").Checkers(iffy.ExpectStatus(200), expectString("param-explode-string", "x,y,z")) + tester.AddCall("query-explode-default", "GET", "/query?param=foo", "").Checkers(iffy.ExpectStatus(200), expectStringArr("param-explode-default", "1", "2", "3")) // default with explode + tester.AddCall("query-explode-disabled-default", "GET", "/query?param=foo", "").Checkers(iffy.ExpectStatus(200), expectStringArr("param-explode-disabled-default", "1,2,3")) // default without explode tester.Run() } @@ -148,16 +151,19 @@ func pathHandler(c *gin.Context, in *pathIn) (*pathIn, error) { } type queryIn struct { - Param string `query:"param" json:"param" validate:"required"` - ParamOptional string `query:"param-optional" json:"param-optional"` - Params []string `query:"params" json:"params"` - ParamInt int `query:"param-int" json:"param-int"` - ParamBool bool `query:"param-bool" json:"param-bool"` - ParamDefault string `query:"param-default" json:"param-default" default:"default" validate:"required"` - ParamPtr *string `query:"param-ptr" json:"param-ptr"` - ParamComplex time.Time `query:"param-complex" json:"param-complex"` - ParamExplode []string `query:"param-explode" json:"param-explode" explode:"true"` - ParamExplodeDisabled []string `query:"param-explode-disabled" json:"param-explode-disabled" explode:"false"` + Param string `query:"param" json:"param" validate:"required"` + ParamOptional string `query:"param-optional" json:"param-optional"` + Params []string `query:"params" json:"params"` + ParamInt int `query:"param-int" json:"param-int"` + ParamBool bool `query:"param-bool" json:"param-bool"` + ParamDefault string `query:"param-default" json:"param-default" default:"default" validate:"required"` + ParamPtr *string `query:"param-ptr" json:"param-ptr"` + ParamComplex time.Time `query:"param-complex" json:"param-complex"` + ParamExplode []string `query:"param-explode" json:"param-explode" explode:"true"` + ParamExplodeDisabled []string `query:"param-explode-disabled" json:"param-explode-disabled" explode:"false"` + ParamExplodeString string `query:"param-explode-string" json:"param-explode-string" explode:"true"` + ParamExplodeDefault []string `query:"param-explode-default" json:"param-explode-default" default:"1,2,3" explode:"true"` + ParamExplodeDefaultDisabled []string `query:"param-explode-disabled-default" json:"param-explode-disabled-default" default:"1,2,3" explode:"false"` *DoubleEmbedded }