Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added required param in BindStyledParameterWithLocation #19

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bindform.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RequestBodyEncoding struct {
ContentType string
Style string
Explode *bool
Required *bool
}

func BindMultipart(ptr interface{}, reader multipart.Reader) error {
Expand Down Expand Up @@ -64,7 +65,11 @@ func BindForm(ptr interface{}, form map[string][]string, files map[string][]*mul
if encoding.Explode != nil {
explode = *encoding.Explode
}
if err := BindStyledParameterWithLocation(encoding.Style, explode, tag, ParamLocationUndefined, value, field.Addr().Interface()); err != nil {
var required bool
if encoding.Required != nil {
required = *encoding.Required
}
if err := BindStyledParameterWithLocation(encoding.Style, explode, required, tag, ParamLocationUndefined, value, field.Addr().Interface()); err != nil {
return err
}
}
Expand Down
12 changes: 7 additions & 5 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ import (
// https://swagger.io/docs/specification/serialization/
// It is a backward compatible function to clients generated with codegen
// up to version v1.5.5. v1.5.6+ calls the function below.
func BindStyledParameter(style string, explode bool, paramName string,
func BindStyledParameter(style string, explode bool, required bool, paramName string,
value string, dest interface{}) error {
return BindStyledParameterWithLocation(style, explode, paramName, ParamLocationUndefined, value, dest)
return BindStyledParameterWithLocation(style, explode, required, paramName, ParamLocationUndefined, value, dest)
}

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

if value == "" {
return fmt.Errorf("parameter '%s' is empty, can't bind its value", paramName)
if required {
if value == "" {
return fmt.Errorf("parameter '%s' is empty, can't bind its value", paramName)
}
}

// Based on the location of the parameter, we need to unescape it properly.
Expand Down
2 changes: 1 addition & 1 deletion bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func TestBindStyledParameterWithLocation(t *testing.T) {
expectedBig := big.NewInt(12345678910)

var dstBigNumber big.Int
err := BindStyledParameterWithLocation("simple", false, "id", ParamLocationUndefined,
err := BindStyledParameterWithLocation("simple", false, false, "id", ParamLocationUndefined,
"12345678910", &dstBigNumber)
assert.NoError(t, err)
assert.Equal(t, *expectedBig, dstBigNumber)
Expand Down