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: make BindQueryParameter play along with x-go-type-skip-optional-pointer #47

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
12 changes: 8 additions & 4 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
// inner code will bind the string's value to this interface.
var output interface{}

if required {
// 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 {
// 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 @@ -414,9 +417,10 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
if err != nil {
return err
}
// If the parameter is required, and we've successfully unmarshaled
// it, this assigns the new object to the pointer pointer.
if !required {
// 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 {
dv.Set(reflect.ValueOf(output))
}
return nil
Expand Down
10 changes: 10 additions & 0 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ func TestBindQueryParameter(t *testing.T) {
queryParams := url.Values{
"time": {"2020-12-09T16:09:53+00:00"},
"number": {"100"},
"text": {"loremipsum"},
}
// An optional time will be a pointer to a time in a parameter object
var optionalTime *time.Time
Expand All @@ -351,6 +352,15 @@ func TestBindQueryParameter(t *testing.T) {
require.NoError(t, err)
assert.Nil(t, optionalNumber)

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

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

// If we require values, we require errors when they're not present.
err = BindQueryParameter("form", true, true, "notfound", queryParams, &optionalTime)
assert.Error(t, err)
Expand Down