@@ -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 }
0 commit comments