Skip to content

Commit ee84079

Browse files
mromaszewiczclaude
andauthored
fix: honor Binder behind an optional pointer (#198)
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 <noreply@anthropic.com>
1 parent fb97b97 commit ee84079

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎bindstring.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ func BindStringToObjectWithOptions(src string, dst interface{}, opts BindStringT
9898

9999
v = reflect.Indirect(v)
100100
t = v.Type()
101+
102+
// The Binder check above was made on a **T, which never implements
103+
// Binder. Now that we have the T behind an optional field, give it
104+
// the same precedence over the built in conversions that it has
105+
// when the field is not optional.
106+
if v.CanAddr() {
107+
if binder, ok := v.Addr().Interface().(Binder); ok {
108+
return binder.Bind(src)
109+
}
110+
}
101111
}
102112

103113
// The resulting type must be settable. reflect will catch issues like

‎bindstring_binder_test.go‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package runtime
2+
3+
import (
4+
"errors"
5+
"net/url"
6+
"strconv"
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/oapi-codegen/runtime/types"
15+
)
16+
17+
// percentBinder is an integer which does its own parsing: it tolerates a
18+
// trailing percent sign, and rejects anything over 100.
19+
type percentBinder int
20+
21+
func (p *percentBinder) Bind(src string) error {
22+
n, err := strconv.Atoi(strings.TrimSuffix(src, "%"))
23+
if err != nil {
24+
return err
25+
}
26+
if n > 100 {
27+
return errors.New("over 100")
28+
}
29+
*p = percentBinder(n)
30+
return nil
31+
}
32+
33+
// A Binder takes precedence over the built in conversions whether or not it
34+
// sits behind the pointer of an optional field. The optional case used to
35+
// parse the underlying primitive without calling Bind, which skipped the
36+
// type's validation and refused its syntax.
37+
// https://github.com/oapi-codegen/runtime/issues/196
38+
func TestBindStringToObject_BinderBehindOptionalPointer(t *testing.T) {
39+
t.Run("custom syntax is accepted", func(t *testing.T) {
40+
var dst percentBinder
41+
require.NoError(t, BindStringToObject("50%", &dst))
42+
assert.Equal(t, percentBinder(50), dst)
43+
44+
var opt *percentBinder
45+
require.NoError(t, BindStringToObject("50%", &opt))
46+
require.NotNil(t, opt)
47+
assert.Equal(t, percentBinder(50), *opt)
48+
})
49+
50+
t.Run("validation is applied", func(t *testing.T) {
51+
var dst percentBinder
52+
assert.EqualError(t, BindStringToObject("150", &dst), "over 100")
53+
54+
var opt *percentBinder
55+
assert.EqualError(t, BindStringToObject("150", &opt), "over 100")
56+
})
57+
58+
t.Run("string kind", func(t *testing.T) {
59+
var opt *CustomStringBinder
60+
require.NoError(t, BindStringToObject("hello", &opt))
61+
require.NotNil(t, opt)
62+
assert.Equal(t, CustomStringBinder("CUSTOM:hello"), *opt)
63+
})
64+
65+
t.Run("types.Date binds as before", func(t *testing.T) {
66+
var opt *types.Date
67+
require.NoError(t, BindStringToObject("2020-11-06", &opt))
68+
require.NotNil(t, opt)
69+
assert.True(t, time.Date(2020, 11, 6, 0, 0, 0, 0, time.UTC).Equal(opt.Time))
70+
71+
var empty *types.Date
72+
require.NoError(t, BindStringToObject("", &empty))
73+
require.NotNil(t, empty)
74+
assert.True(t, empty.IsZero())
75+
76+
var bad *types.Date
77+
require.Error(t, BindStringToObject("not-a-date", &bad))
78+
})
79+
80+
t.Run("types without a Binder are unaffected", func(t *testing.T) {
81+
var opt *int
82+
require.NoError(t, BindStringToObject("150", &opt))
83+
require.NotNil(t, opt)
84+
assert.Equal(t, 150, *opt)
85+
86+
var bad *int
87+
require.Error(t, BindStringToObject("50%", &bad))
88+
})
89+
90+
t.Run("optional property of an exploded form object", func(t *testing.T) {
91+
type Filters struct {
92+
Required percentBinder `json:"required,omitempty"`
93+
Optional *percentBinder `json:"optional,omitempty"`
94+
}
95+
96+
var dst *Filters
97+
values := url.Values{"required": {"25%"}, "optional": {"50%"}}
98+
require.NoError(t, BindQueryParameter("form", true, false, "filters", values, &dst))
99+
require.NotNil(t, dst)
100+
assert.Equal(t, percentBinder(25), dst.Required)
101+
require.NotNil(t, dst.Optional)
102+
assert.Equal(t, percentBinder(50), *dst.Optional)
103+
104+
dst = nil
105+
values = url.Values{"optional": {"150"}}
106+
err := BindQueryParameter("form", true, false, "filters", values, &dst)
107+
require.Error(t, err)
108+
assert.Contains(t, err.Error(), "over 100")
109+
})
110+
}

0 commit comments

Comments
 (0)