Skip to content

Commit 53c0dbc

Browse files
deepmap-marcinrmarcinromaszewicz
and
marcinromaszewicz
authored
Fix breaking changes recently committed (#20)
Pull request #19 introduced an extra parameter into a widely used function, which would break existing generated code with the next release. Restore the previous function to its old signature, and add a new one which takes additional options in an options structure. Co-authored-by: marcinromaszewicz <[email protected]>
1 parent 7290e8f commit 53c0dbc

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

bindform.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ func BindForm(ptr interface{}, form map[string][]string, files map[string][]*mul
6969
if encoding.Required != nil {
7070
required = *encoding.Required
7171
}
72-
if err := BindStyledParameterWithLocation(encoding.Style, explode, required, tag, ParamLocationUndefined, value, field.Addr().Interface()); err != nil {
72+
if err := BindStyledParameterWithOptions(encoding.Style, tag, value, field.Addr().Interface(), BindStyledParameterOptions{
73+
ParamLocation: ParamLocationUndefined,
74+
Explode: explode,
75+
Required: required,
76+
}); err != nil {
7377
return err
7478
}
7579
}

bindparam.go

+35-8
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,53 @@ import (
3131
// https://swagger.io/docs/specification/serialization/
3232
// It is a backward compatible function to clients generated with codegen
3333
// up to version v1.5.5. v1.5.6+ calls the function below.
34-
func BindStyledParameter(style string, explode bool, required bool, paramName string,
34+
// Deprecated: BindStyledParameter is deprecated.
35+
func BindStyledParameter(style string, explode bool, paramName string,
3536
value string, dest interface{}) error {
36-
return BindStyledParameterWithLocation(style, explode, required, paramName, ParamLocationUndefined, value, dest)
37+
return BindStyledParameterWithOptions(style, paramName, value, dest, BindStyledParameterOptions{
38+
ParamLocation: ParamLocationUndefined,
39+
Explode: explode,
40+
Required: true,
41+
})
3742
}
3843

3944
// BindStyledParameterWithLocation binds a parameter as described in the Path Parameters
4045
// section here to a Go object:
4146
// https://swagger.io/docs/specification/serialization/
42-
func BindStyledParameterWithLocation(style string, explode bool, required bool, paramName string,
47+
// This is a compatibility function which is used by oapi-codegen v2.0.0 and earlier.
48+
// Deprecated: BindStyledParameterWithLocation is deprecated.
49+
func BindStyledParameterWithLocation(style string, explode bool, paramName string,
4350
paramLocation ParamLocation, value string, dest interface{}) error {
51+
return BindStyledParameterWithOptions(style, paramName, value, dest, BindStyledParameterOptions{
52+
ParamLocation: paramLocation,
53+
Explode: explode,
54+
Required: true, // This emulates behavior before the required parameter was optional.
55+
})
56+
}
4457

45-
if required {
58+
// BindStyledParameterOptions defines optional arguments for BindStyledParameterWithOptions
59+
type BindStyledParameterOptions struct {
60+
// ParamLocation tells us where the parameter is located in the request.
61+
ParamLocation ParamLocation
62+
// Whether the parameter should use exploded structure
63+
Explode bool
64+
// Whether the parameter is required in the query
65+
Required bool
66+
}
67+
68+
// BindStyledParameterWithOptions binds a parameter as described in the Path Parameters
69+
// section here to a Go object:
70+
// https://swagger.io/docs/specification/serialization/
71+
func BindStyledParameterWithOptions(style string, paramName string, value string, dest any, opts BindStyledParameterOptions) error {
72+
if opts.Required {
4673
if value == "" {
4774
return fmt.Errorf("parameter '%s' is empty, can't bind its value", paramName)
4875
}
4976
}
5077

5178
// Based on the location of the parameter, we need to unescape it properly.
5279
var err error
53-
switch paramLocation {
80+
switch opts.ParamLocation {
5481
case ParamLocationQuery, ParamLocationUndefined:
5582
// We unescape undefined parameter locations here for older generated code,
5683
// since prior to this refactoring, they always query unescaped.
@@ -85,17 +112,17 @@ func BindStyledParameterWithLocation(style string, explode bool, required bool,
85112
if t.Kind() == reflect.Struct {
86113
// We've got a destination object, we'll create a JSON representation
87114
// of the input value, and let the json library deal with the unmarshaling
88-
parts, err := splitStyledParameter(style, explode, true, paramName, value)
115+
parts, err := splitStyledParameter(style, opts.Explode, true, paramName, value)
89116
if err != nil {
90117
return err
91118
}
92119

93-
return bindSplitPartsToDestinationStruct(paramName, parts, explode, dest)
120+
return bindSplitPartsToDestinationStruct(paramName, parts, opts.Explode, dest)
94121
}
95122

96123
if t.Kind() == reflect.Slice {
97124
// Chop up the parameter into parts based on its style
98-
parts, err := splitStyledParameter(style, explode, false, paramName, value)
125+
parts, err := splitStyledParameter(style, opts.Explode, false, paramName, value)
99126
if err != nil {
100127
return fmt.Errorf("error splitting input '%s' into parts: %s", value, err)
101128
}

bindparam_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,12 @@ func TestBindStyledParameterWithLocation(t *testing.T) {
493493
expectedBig := big.NewInt(12345678910)
494494

495495
var dstBigNumber big.Int
496-
err := BindStyledParameterWithLocation("simple", false, false, "id", ParamLocationUndefined,
497-
"12345678910", &dstBigNumber)
496+
497+
err := BindStyledParameterWithOptions("simple", "id", "12345678910", &dstBigNumber, BindStyledParameterOptions{
498+
ParamLocation: ParamLocationUndefined,
499+
Explode: false,
500+
Required: false,
501+
})
498502
assert.NoError(t, err)
499503
assert.Equal(t, *expectedBig, dstBigNumber)
500504
}

0 commit comments

Comments
 (0)