diff --git a/query/encode.go b/query/encode.go index c936954..3b058bb 100644 --- a/query/encode.go +++ b/query/encode.go @@ -186,6 +186,11 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error { continue } + // unwrap interface values so the concrete type's Encoder is used + if sv.Kind() == reflect.Interface && !sv.IsNil() { + sv = sv.Elem() + } + if sv.Type().Implements(encoderType) { // if sv is a nil pointer and the custom encoder is defined on a non-pointer // method receiver, set sv to the zero value of the underlying type diff --git a/query/encode_test.go b/query/encode_test.go index a94b44d..241e4f1 100644 --- a/query/encode_test.go +++ b/query/encode_test.go @@ -477,6 +477,14 @@ func TestValues_CustomEncodingSlice(t *testing.T) { }{(*customEncodedStrings)(&[]string{"a", "b"})}, url.Values{"v.0": {"a"}, "v.1": {"b"}}, }, + + // custom encoded type held in an interface field + { + struct { + V interface{} `url:"v"` + }{customEncodedStrings{"a", "b"}}, + url.Values{"v.0": {"a"}, "v.1": {"b"}}, + }, } for _, tt := range tests {