@@ -31,26 +31,53 @@ import (
31
31
// https://swagger.io/docs/specification/serialization/
32
32
// It is a backward compatible function to clients generated with codegen
33
33
// 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 ,
35
36
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
+ })
37
42
}
38
43
39
44
// BindStyledParameterWithLocation binds a parameter as described in the Path Parameters
40
45
// section here to a Go object:
41
46
// 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 ,
43
50
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
+ }
44
57
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 {
46
73
if value == "" {
47
74
return fmt .Errorf ("parameter '%s' is empty, can't bind its value" , paramName )
48
75
}
49
76
}
50
77
51
78
// Based on the location of the parameter, we need to unescape it properly.
52
79
var err error
53
- switch paramLocation {
80
+ switch opts . ParamLocation {
54
81
case ParamLocationQuery , ParamLocationUndefined :
55
82
// We unescape undefined parameter locations here for older generated code,
56
83
// since prior to this refactoring, they always query unescaped.
@@ -85,17 +112,17 @@ func BindStyledParameterWithLocation(style string, explode bool, required bool,
85
112
if t .Kind () == reflect .Struct {
86
113
// We've got a destination object, we'll create a JSON representation
87
114
// 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 )
89
116
if err != nil {
90
117
return err
91
118
}
92
119
93
- return bindSplitPartsToDestinationStruct (paramName , parts , explode , dest )
120
+ return bindSplitPartsToDestinationStruct (paramName , parts , opts . Explode , dest )
94
121
}
95
122
96
123
if t .Kind () == reflect .Slice {
97
124
// 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 )
99
126
if err != nil {
100
127
return fmt .Errorf ("error splitting input '%s' into parts: %s" , value , err )
101
128
}
0 commit comments