Skip to content

Commit ef9a89b

Browse files
committed
Fix: deepObject return without assign on !required
1 parent 409f622 commit ef9a89b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

bindparam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ func BindQueryParameter(style string, explode bool, required bool, paramName str
469469
if !explode {
470470
return errors.New("deepObjects must be exploded")
471471
}
472-
return UnmarshalDeepObject(dest, paramName, queryParams)
472+
return unmarshalDeepObject(dest, paramName, queryParams, required)
473473
case "spaceDelimited", "pipeDelimited":
474474
return fmt.Errorf("query arguments of style '%s' aren't yet supported", style)
475475
default:

bindparam_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ func TestBindQueryParameter(t *testing.T) {
322322
err := BindQueryParameter("deepObject", true, false, paramName, queryParams, &actual)
323323
assert.NoError(t, err)
324324
assert.Equal(t, expectedDeepObject, actual)
325+
326+
// If we require values, we require errors when they're not present.
327+
err = BindQueryParameter("deepObject", true, true, "notfound", queryParams, &actual)
328+
assert.Error(t, err)
325329
})
326330

327331
t.Run("form", func(t *testing.T) {

deepobject.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func makeFieldOrValue(paths [][]string, values []string) fieldOrValue {
124124
}
125125

126126
func UnmarshalDeepObject(dst interface{}, paramName string, params url.Values) error {
127+
return unmarshalDeepObject(dst, paramName, params, false)
128+
}
129+
130+
func unmarshalDeepObject(dst interface{}, paramName string, params url.Values, required bool) error {
127131
// Params are all the query args, so we need those that look like
128132
// "paramName["...
129133
var fieldNames []string
@@ -141,6 +145,14 @@ func UnmarshalDeepObject(dst interface{}, paramName string, params url.Values) e
141145
}
142146
}
143147

148+
if len(fieldNames) == 0 {
149+
if required {
150+
return fmt.Errorf("query parameter '%s' is required", paramName)
151+
} else {
152+
return nil
153+
}
154+
}
155+
144156
// Now, for each field, reconstruct its subscript path and value
145157
paths := make([][]string, len(fieldNames))
146158
for i, path := range fieldNames {

0 commit comments

Comments
 (0)