Skip to content

Commit

Permalink
Merge pull request #48 from TelpeNight/fix-opt-query-params
Browse files Browse the repository at this point in the history
Fix BindQueryParameter for optional parameters
  • Loading branch information
mromaszewicz authored Jul 9, 2024
2 parents 8c8696b + fb5c804 commit 912bc9f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
// required params are never pointers, but it may happen that optional param
// is not pointer as well if user decides to annotate it with
// x-go-type-skip-optional-pointer
if required || v.Kind() != reflect.Pointer {
var extraIndirect = !required && v.Kind() == reflect.Pointer
if !extraIndirect {
// If the parameter is required, then the generated code will pass us
// a pointer to it: &int, &object, and so forth. We can directly set
// them.
Expand Down Expand Up @@ -420,7 +421,7 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
// If the parameter is required (or relies on x-go-type-skip-optional-pointer),
// and we've successfully unmarshaled it, this assigns the new object to the
// pointer pointer.
if !required && k == reflect.Pointer {
if extraIndirect {
dv.Set(reflect.ValueOf(output))
}
return nil
Expand Down Expand Up @@ -460,7 +461,7 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if err != nil {
return err
}
if !required {
if extraIndirect {
dv.Set(reflect.ValueOf(output))
}
return nil
Expand Down
11 changes: 10 additions & 1 deletion bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func TestBindQueryParameter(t *testing.T) {
var optionalNonPointerText = ""
err = BindQueryParameter("form", true, false, "notfound", queryParams, &optionalNonPointerText)
require.NoError(t, err)
assert.Zero(t, "")
assert.Zero(t, optionalNonPointerText)

err = BindQueryParameter("form", true, false, "text", queryParams, &optionalNonPointerText)
require.NoError(t, err)
Expand All @@ -367,6 +367,15 @@ func TestBindQueryParameter(t *testing.T) {
err = BindQueryParameter("form", true, true, "notfound", queryParams, &optionalNumber)
assert.Error(t, err)

var optionalPointerText *string
err = BindQueryParameter("form", true, false, "notfound", queryParams, &optionalPointerText)
require.NoError(t, err)
assert.Zero(t, optionalPointerText)

err = BindQueryParameter("form", true, false, "text", queryParams, &optionalPointerText)
require.NoError(t, err)
require.NotNil(t, optionalPointerText)
assert.Equal(t, "loremipsum", *optionalPointerText)
})
}

Expand Down

0 comments on commit 912bc9f

Please sign in to comment.