Skip to content
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
19 changes: 14 additions & 5 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,15 @@ func BindQueryParameterWithOptions(style string, explode bool, required bool, pa
// try to bind field by field.
var fieldsPresent bool
fieldsPresent, err = bindParamsToExplodedObject(paramName, queryParams, output)
// If no fields were set, and there is no error, we will not fall
// through to assign the destination. An absent required
// parameter is an error, matching the slice and primitive cases
// above; this also covers scalar struct types such as
// types.Date and time.Time.
// A field which is present but can't be bound is an error,
// and must not be mistaken for an absent parameter.
if err != nil {
return err
}
// If no fields were set, we will not fall through to assign
// the destination. An absent required parameter is an error,
// matching the slice and primitive cases above; this also
// covers scalar struct types such as types.Date and time.Time.
if !fieldsPresent {
if required {
return &RequiredParameterError{ParamName: paramName}
Expand Down Expand Up @@ -778,6 +782,11 @@ func BindRawQueryParameter(style string, explode bool, required bool, paramName
case reflect.Struct:
var fieldsPresent bool
fieldsPresent, err = bindParamsToExplodedObject(paramName, queryParams, output)
// A field which is present but can't be bound is an error,
// and must not be mistaken for an absent parameter.
if err != nil {
return err
}
// An absent required parameter is an error, matching the slice
// and primitive cases above; this also covers scalar struct
// types such as types.Date and time.Time.
Expand Down
76 changes: 76 additions & 0 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,3 +1906,79 @@ func TestRoundTripPathParameter(t *testing.T) {
})
}
}

// A property of an exploded form object which is present in the query string,
// but can't be bound, is an error. It used to be reported as though the whole
// parameter were absent: silently for an optional parameter, and as a
// RequiredParameterError for a required one.
// https://github.com/oapi-codegen/runtime/issues/193
func TestBindExplodedObjectFieldErrors(t *testing.T) {
type QueryFilters struct {
Limit *int `json:"limit,omitempty"`
Cursor *string `json:"cursor,omitempty"`
}

// Generated servers reach the exploded object binder through either of
// these, so each case runs against both.
binders := map[string]func(required bool, rawQuery string, dest interface{}) error{
"BindQueryParameter": func(required bool, rawQuery string, dest interface{}) error {
values, err := url.ParseQuery(rawQuery)
require.NoError(t, err)
return BindQueryParameter("form", true, required, "queryFilters", values, dest)
},
"BindRawQueryParameter": func(required bool, rawQuery string, dest interface{}) error {
return BindRawQueryParameter("form", true, required, "queryFilters", rawQuery, dest)
},
}

badQueries := map[string]string{
"value which does not parse": "limit=abc",
"primitive specified twice": "limit=1&limit=2",
"bad value beside a good one": "cursor=xyz&limit=abc",
"good value before a bad one": "limit=1&cursor=a&cursor=b",
"unrelated arguments are ignored": "other=1&limit=abc",
}

for binderName, bind := range binders {
for name, rawQuery := range badQueries {
t.Run(binderName+"/optional/"+name, func(t *testing.T) {
var dst *QueryFilters
err := bind(false, rawQuery, &dst)
require.Error(t, err)
assert.Contains(t, err.Error(), "queryFilters")
assert.Nil(t, dst)
})

t.Run(binderName+"/required/"+name, func(t *testing.T) {
var dst QueryFilters
err := bind(true, rawQuery, &dst)
require.Error(t, err)
assert.Contains(t, err.Error(), "queryFilters")
var requiredErr *RequiredParameterError
assert.NotErrorAs(t, err, &requiredErr)
})
}

t.Run(binderName+"/optional/absent is not an error", func(t *testing.T) {
var dst *QueryFilters
require.NoError(t, bind(false, "other=1", &dst))
assert.Nil(t, dst)
})

t.Run(binderName+"/required/absent is a RequiredParameterError", func(t *testing.T) {
var dst QueryFilters
err := bind(true, "other=1", &dst)
var requiredErr *RequiredParameterError
require.ErrorAs(t, err, &requiredErr)
assert.Equal(t, "queryFilters", requiredErr.ParamName)
})

t.Run(binderName+"/valid fields still bind", func(t *testing.T) {
var dst *QueryFilters
require.NoError(t, bind(false, "limit=5&cursor=xyz", &dst))
require.NotNil(t, dst)
assert.Equal(t, 5, *dst.Limit)
assert.Equal(t, "xyz", *dst.Cursor)
})
}
}