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
48 changes: 48 additions & 0 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,17 @@ func bindParamsToExplodedObject(paramName string, values url.Values, dest interf
// At this point, we look up field name in the parameter list.
fieldVal, found := values[fieldName]
if found {
// An array property of an exploded object is serialized the same
// way as a top level exploded array: the property name repeats
// once per element.
if isExplodedObjectArrayField(fieldT.Type) {
err := bindExplodedObjectArrayField(fieldVal, v.Field(i))
if err != nil {
return false, fmt.Errorf("could not bind query arg '%s' to request object: %s'", paramName, err)
}
fieldsPresent = true
continue
}
if len(fieldVal) != 1 {
return false, fmt.Errorf("field '%s' specified multiple times for param '%s'", fieldName, paramName)
}
Expand All @@ -956,6 +967,43 @@ func bindParamsToExplodedObject(paramName string, values url.Values, dest interf
return fieldsPresent, nil
}

// isExplodedObjectArrayField reports whether a field of an exploded object
// binds as an array, taking one element from each occurrence of its name in
// the query string. That is any slice, or pointer to a slice, apart from the
// ones which bind from a single string: byte slices, and types which do their
// own parsing via Binder or encoding.TextUnmarshaler.
func isExplodedObjectArrayField(t reflect.Type) bool {
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
if t.Kind() != reflect.Slice || isByteSlice(t) {
return false
}
pt := reflect.PointerTo(t)
return !pt.Implements(reflect.TypeFor[Binder]()) &&
!pt.Implements(reflect.TypeFor[encoding.TextUnmarshaler]())
}

// bindExplodedObjectArrayField binds one query string value per element into
// field, which must satisfy isExplodedObjectArrayField. A nil pointer field
// is allocated, and the field is left untouched on error.
func bindExplodedObjectArrayField(values []string, field reflect.Value) error {
t := field.Type()
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
slice := reflect.New(t)
if err := bindSplitPartsToDestinationArray(values, slice.Interface()); err != nil {
return err
}
if field.Kind() == reflect.Pointer {
field.Set(slice)
} else {
field.Set(slice.Elem())
}
return nil
}

// indirect
func indirect(dest interface{}) (interface{}, reflect.Value, reflect.Type) {
v := reflect.ValueOf(dest)
Expand Down
124 changes: 124 additions & 0 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"encoding/json"
"fmt"
"math/big"
"net"
"net/url"
"reflect"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -869,6 +871,128 @@ func TestBindParamsToExplodedObject(t *testing.T) {
assert.EqualValues(t, &now, optDstTime.Time)
}

// explodedObjectTags binds from a single string even though it is a slice.
type explodedObjectTags []string

func (e *explodedObjectTags) Bind(src string) error {
*e = strings.Split(src, "|")
return nil
}

// explodedObjectWords is a slice which parses itself from a single string.
type explodedObjectWords []string

func (e *explodedObjectWords) UnmarshalText(text []byte) error {
*e = strings.Fields(string(text))
return nil
}

func TestIsExplodedObjectArrayField(t *testing.T) {
assert.True(t, isExplodedObjectArrayField(reflect.TypeFor[[]string]()))
assert.True(t, isExplodedObjectArrayField(reflect.TypeFor[*[]int]()))

assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[string]()))
assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[*int]()))
assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[[]byte]()))
assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[net.IP]()))
assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[explodedObjectTags]()))
assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[*explodedObjectTags]()))
assert.False(t, isExplodedObjectArrayField(reflect.TypeFor[explodedObjectWords]()))
}

// An array property of an exploded form object repeats its name once per
// element, like a top level exploded array does.
// https://github.com/oapi-codegen/runtime/issues/193
func TestBindExplodedObjectArrayFields(t *testing.T) {
type QueryFilters struct {
Limit *int `json:"limit,omitempty"`
OrderBy *[]string `json:"orderBy,omitempty"`
IDs []int `json:"ids,omitempty"`
Tags explodedObjectTags `json:"tags,omitempty"`
}

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

limit := 5
testCases := []struct {
name string
rawQuery string
expected QueryFilters
}{
{
name: "multiple values",
rawQuery: "orderBy=foo&orderBy=bar",
expected: QueryFilters{OrderBy: &[]string{"foo", "bar"}},
},
{
name: "single value",
rawQuery: "orderBy=foo",
expected: QueryFilters{OrderBy: &[]string{"foo"}},
},
{
name: "alongside a primitive property",
rawQuery: "limit=5&orderBy=foo&orderBy=bar",
expected: QueryFilters{Limit: &limit, OrderBy: &[]string{"foo", "bar"}},
},
{
name: "commas are not delimiters",
rawQuery: "orderBy=foo,bar&orderBy=baz",
expected: QueryFilters{OrderBy: &[]string{"foo,bar", "baz"}},
},
{
name: "non-pointer slice of integers",
rawQuery: "ids=3&ids=1&ids=2",
expected: QueryFilters{IDs: []int{3, 1, 2}},
},
{
name: "Binder slice binds from one string",
rawQuery: "tags=a|b",
expected: QueryFilters{Tags: explodedObjectTags{"a", "b"}},
},
}

for binderName, bind := range binders {
for _, tc := range testCases {
t.Run(binderName+"/"+tc.name, func(t *testing.T) {
var dst *QueryFilters
require.NoError(t, bind(tc.rawQuery, &dst))
require.NotNil(t, dst)
assert.Equal(t, tc.expected, *dst)
})
}
}

t.Run("element which does not parse", func(t *testing.T) {
var dst QueryFilters
_, err := bindParamsToExplodedObject("queryFilters", url.Values{"ids": {"1", "two"}}, &dst)
require.Error(t, err)
assert.Nil(t, dst.IDs)
})

t.Run("repeated primitive property is still an error", func(t *testing.T) {
var dst QueryFilters
_, err := bindParamsToExplodedObject("queryFilters", url.Values{"limit": {"1", "2"}}, &dst)
assert.EqualError(t, err, "field 'limit' specified multiple times for param 'queryFilters'")
})

t.Run("repeated Binder slice is still an error", func(t *testing.T) {
var dst QueryFilters
_, err := bindParamsToExplodedObject("queryFilters", url.Values{"tags": {"a", "b"}}, &dst)
assert.EqualError(t, err, "field 'tags' specified multiple times for param 'queryFilters'")
})
}

func TestFindRawQueryParam(t *testing.T) {
tests := []struct {
name string
Expand Down