Skip to content

Commit 11ea4d6

Browse files
Merge pull request #19 from itpavelkozlov/main
Added required param in BindStyledParameterWithLocation
2 parents 36b4f92 + 51c76c5 commit 11ea4d6

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

bindform.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type RequestBodyEncoding struct {
2020
ContentType string
2121
Style string
2222
Explode *bool
23+
Required *bool
2324
}
2425

2526
func BindMultipart(ptr interface{}, reader multipart.Reader) error {
@@ -64,7 +65,11 @@ func BindForm(ptr interface{}, form map[string][]string, files map[string][]*mul
6465
if encoding.Explode != nil {
6566
explode = *encoding.Explode
6667
}
67-
if err := BindStyledParameterWithLocation(encoding.Style, explode, tag, ParamLocationUndefined, value, field.Addr().Interface()); err != nil {
68+
var required bool
69+
if encoding.Required != nil {
70+
required = *encoding.Required
71+
}
72+
if err := BindStyledParameterWithLocation(encoding.Style, explode, required, tag, ParamLocationUndefined, value, field.Addr().Interface()); err != nil {
6873
return err
6974
}
7075
}

bindparam.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ 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, paramName string,
34+
func BindStyledParameter(style string, explode bool, required bool, paramName string,
3535
value string, dest interface{}) error {
36-
return BindStyledParameterWithLocation(style, explode, paramName, ParamLocationUndefined, value, dest)
36+
return BindStyledParameterWithLocation(style, explode, required, paramName, ParamLocationUndefined, value, dest)
3737
}
3838

3939
// BindStyledParameterWithLocation binds a parameter as described in the Path Parameters
4040
// section here to a Go object:
4141
// https://swagger.io/docs/specification/serialization/
42-
func BindStyledParameterWithLocation(style string, explode bool, paramName string,
42+
func BindStyledParameterWithLocation(style string, explode bool, required bool, paramName string,
4343
paramLocation ParamLocation, value string, dest interface{}) error {
4444

45-
if value == "" {
46-
return fmt.Errorf("parameter '%s' is empty, can't bind its value", paramName)
45+
if required {
46+
if value == "" {
47+
return fmt.Errorf("parameter '%s' is empty, can't bind its value", paramName)
48+
}
4749
}
4850

4951
// Based on the location of the parameter, we need to unescape it properly.

bindparam_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func TestBindStyledParameterWithLocation(t *testing.T) {
493493
expectedBig := big.NewInt(12345678910)
494494

495495
var dstBigNumber big.Int
496-
err := BindStyledParameterWithLocation("simple", false, "id", ParamLocationUndefined,
496+
err := BindStyledParameterWithLocation("simple", false, false, "id", ParamLocationUndefined,
497497
"12345678910", &dstBigNumber)
498498
assert.NoError(t, err)
499499
assert.Equal(t, *expectedBig, dstBigNumber)

0 commit comments

Comments
 (0)