From 7d102609952c5122833583d50edc94f5633f0dc2 Mon Sep 17 00:00:00 2001 From: Marcin Romaszewicz Date: Sun, 22 Nov 2026 16:14:17 -0800 Subject: [PATCH] fix: honor Binder behind an optional pointer BindStringToObject checks for Binder on the destination as passed. For an optional field that is a **T, which never implements Binder, so the type fell through to the built-in conversion for its kind. An integer type with a Bind method was parsed as a plain integer: its validation was skipped and any syntax of its own was refused. The same type bound through Bind as a required field or as a top-level optional parameter. Check again once the optional pointer has been dereferenced, so that a Binder has the same precedence in both cases. This changes the result for an optional property of an exploded form object whose type has a primitive kind and implements Binder: values which Bind rejects are now errors, values only Bind understands now bind, and a Bind which transforms its input now takes effect. Types without a Bind method are unaffected. types.Date binds the same values as before, though a malformed date now reports the error from Date.Bind. Part of #196 Co-Authored-By: Claude Fable 5.2 --- bindstring.go | 10 ++++ bindstring_binder_test.go | 110 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 bindstring_binder_test.go diff --git a/bindstring.go b/bindstring.go index dd2a63e..0220985 100644 --- a/bindstring.go +++ b/bindstring.go @@ -93,6 +93,16 @@ func BindStringToObjectWithOptions(src string, dst interface{}, opts BindStringT v = reflect.Indirect(v) t = v.Type() + + // The Binder check above was made on a **T, which never implements + // Binder. Now that we have the T behind an optional field, give it + // the same precedence over the built in conversions that it has + // when the field is not optional. + if v.CanAddr() { + if binder, ok := v.Addr().Interface().(Binder); ok { + return binder.Bind(src) + } + } } // The resulting type must be settable. reflect will catch issues like diff --git a/bindstring_binder_test.go b/bindstring_binder_test.go new file mode 100644 index 0000000..1bc20e2 --- /dev/null +++ b/bindstring_binder_test.go @@ -0,0 +1,110 @@ +package runtime + +import ( + "errors" + "net/url" + "strconv" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/oapi-codegen/runtime/types" +) + +// percentBinder is an integer which does its own parsing: it tolerates a +// trailing percent sign, and rejects anything over 100. +type percentBinder int + +func (p *percentBinder) Bind(src string) error { + n, err := strconv.Atoi(strings.TrimSuffix(src, "%")) + if err != nil { + return err + } + if n > 100 { + return errors.New("over 100") + } + *p = percentBinder(n) + return nil +} + +// A Binder takes precedence over the built in conversions whether or not it +// sits behind the pointer of an optional field. The optional case used to +// parse the underlying primitive without calling Bind, which skipped the +// type's validation and refused its syntax. +// https://github.com/oapi-codegen/runtime/issues/196 +func TestBindStringToObject_BinderBehindOptionalPointer(t *testing.T) { + t.Run("custom syntax is accepted", func(t *testing.T) { + var dst percentBinder + require.NoError(t, BindStringToObject("50%", &dst)) + assert.Equal(t, percentBinder(50), dst) + + var opt *percentBinder + require.NoError(t, BindStringToObject("50%", &opt)) + require.NotNil(t, opt) + assert.Equal(t, percentBinder(50), *opt) + }) + + t.Run("validation is applied", func(t *testing.T) { + var dst percentBinder + assert.EqualError(t, BindStringToObject("150", &dst), "over 100") + + var opt *percentBinder + assert.EqualError(t, BindStringToObject("150", &opt), "over 100") + }) + + t.Run("string kind", func(t *testing.T) { + var opt *CustomStringBinder + require.NoError(t, BindStringToObject("hello", &opt)) + require.NotNil(t, opt) + assert.Equal(t, CustomStringBinder("CUSTOM:hello"), *opt) + }) + + t.Run("types.Date binds as before", func(t *testing.T) { + var opt *types.Date + require.NoError(t, BindStringToObject("2020-11-06", &opt)) + require.NotNil(t, opt) + assert.True(t, time.Date(2020, 11, 6, 0, 0, 0, 0, time.UTC).Equal(opt.Time)) + + var empty *types.Date + require.NoError(t, BindStringToObject("", &empty)) + require.NotNil(t, empty) + assert.True(t, empty.IsZero()) + + var bad *types.Date + require.Error(t, BindStringToObject("not-a-date", &bad)) + }) + + t.Run("types without a Binder are unaffected", func(t *testing.T) { + var opt *int + require.NoError(t, BindStringToObject("150", &opt)) + require.NotNil(t, opt) + assert.Equal(t, 150, *opt) + + var bad *int + require.Error(t, BindStringToObject("50%", &bad)) + }) + + t.Run("optional property of an exploded form object", func(t *testing.T) { + type Filters struct { + Required percentBinder `json:"required,omitempty"` + Optional *percentBinder `json:"optional,omitempty"` + } + + var dst *Filters + values := url.Values{"required": {"25%"}, "optional": {"50%"}} + require.NoError(t, BindQueryParameter("form", true, false, "filters", values, &dst)) + require.NotNil(t, dst) + assert.Equal(t, percentBinder(25), dst.Required) + require.NotNil(t, dst.Optional) + assert.Equal(t, percentBinder(50), *dst.Optional) + + dst = nil + values = url.Values{"optional": {"150"}} + err := BindQueryParameter("form", true, false, "filters", values, &dst) + require.Error(t, err) + assert.Contains(t, err.Error(), "over 100") + }) +}