Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BindQueryParameter for optional parameters #48

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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