From 121e633ce5d173859a635f76f0b46e8f258f6721 Mon Sep 17 00:00:00 2001 From: Charlie Tonneslan Date: Mon, 6 Jul 2026 12:09:59 -0400 Subject: [PATCH] query: honor Encoder on interface fields reflectValue checked sv.Type().Implements(encoderType) against the static field type, so a value stored in an interface{} field never used its EncodeValues method and fell back to default formatting. Unwrap the interface first so the concrete type's Encoder is seen. Fixes #121 Signed-off-by: Charlie Tonneslan --- query/encode.go | 5 +++++ query/encode_test.go | 8 ++++++++ 2 files changed, 13 insertions(+) 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 {