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

Fix breaking changes recently committed #20

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion bindform.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ func BindForm(ptr interface{}, form map[string][]string, files map[string][]*mul
if encoding.Required != nil {
required = *encoding.Required
}
if err := BindStyledParameterWithLocation(encoding.Style, explode, required, tag, ParamLocationUndefined, value, field.Addr().Interface()); err != nil {
if err := BindStyledParameterWithOptions(encoding.Style, tag, value, field.Addr().Interface(), BindStyledParameterOptions{
ParamLocation: ParamLocationUndefined,
Explode: explode,
Required: required,
}); err != nil {
return err
}
}
Expand Down
35 changes: 28 additions & 7 deletions bindparam.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,45 @@ import (
// up to version v1.5.5. v1.5.6+ calls the function below.
func BindStyledParameter(style string, explode bool, required bool, paramName string,
value string, dest interface{}) error {
return BindStyledParameterWithLocation(style, explode, required, paramName, ParamLocationUndefined, value, dest)
return BindStyledParameterWithLocation(style, explode, 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, required bool, paramName string,
// This is a compatibility function which is used by oapi-codegen v2.0.0 and earlier.
deepmap-marcinr marked this conversation as resolved.
Show resolved Hide resolved
func BindStyledParameterWithLocation(style string, explode bool, paramName string,
paramLocation ParamLocation, value string, dest interface{}) error {
return BindStyledParameterWithOptions(style, paramName, value, dest, BindStyledParameterOptions{
ParamLocation: paramLocation,
Explode: explode,
Required: true, // This emulates behavior before the required parameter was optional.
})
}

if required {
// BindStyledParameterOptions defines optional arguments for BindStyledParameterWithOptions
type BindStyledParameterOptions struct {
// ParamLocation tells us where the parameter is located in the request.
ParamLocation ParamLocation
// Whether the parameter should use exploded structure
Explode bool
// Whether the parameter is required in the query
Required bool
}

// BindStyledParameterWithOptions binds a parameter as described in the Path Parameters
// section here to a Go object:
// https://swagger.io/docs/specification/serialization/
func BindStyledParameterWithOptions(style string, paramName string, value string, dest any, opts BindStyledParameterOptions) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth having style as an option, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those four args, style, paramName, value, dest are required, which makes them different from the options in the opts struct, where zerovalues are ok.

if opts.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.
var err error
switch paramLocation {
switch opts.ParamLocation {
case ParamLocationQuery, ParamLocationUndefined:
// We unescape undefined parameter locations here for older generated code,
// since prior to this refactoring, they always query unescaped.
Expand Down Expand Up @@ -85,17 +106,17 @@ func BindStyledParameterWithLocation(style string, explode bool, required bool,
if t.Kind() == reflect.Struct {
// We've got a destination object, we'll create a JSON representation
// of the input value, and let the json library deal with the unmarshaling
parts, err := splitStyledParameter(style, explode, true, paramName, value)
parts, err := splitStyledParameter(style, opts.Explode, true, paramName, value)
if err != nil {
return err
}

return bindSplitPartsToDestinationStruct(paramName, parts, explode, dest)
return bindSplitPartsToDestinationStruct(paramName, parts, opts.Explode, dest)
}

if t.Kind() == reflect.Slice {
// Chop up the parameter into parts based on its style
parts, err := splitStyledParameter(style, explode, false, paramName, value)
parts, err := splitStyledParameter(style, opts.Explode, false, paramName, value)
if err != nil {
return fmt.Errorf("error splitting input '%s' into parts: %s", value, err)
}
Expand Down
11 changes: 8 additions & 3 deletions bindparam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import (
"testing"
"time"

"github.com/oapi-codegen/runtime/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/oapi-codegen/runtime/types"
deepmap-marcinr marked this conversation as resolved.
Show resolved Hide resolved
)

// MockBinder is just an independent version of Binder that has the Bind implemented
Expand Down Expand Up @@ -493,8 +494,12 @@ func TestBindStyledParameterWithLocation(t *testing.T) {
expectedBig := big.NewInt(12345678910)

var dstBigNumber big.Int
err := BindStyledParameterWithLocation("simple", false, false, "id", ParamLocationUndefined,
"12345678910", &dstBigNumber)

err := BindStyledParameterWithOptions("simple", "id", "12345678910", &dstBigNumber, BindStyledParameterOptions{
ParamLocation: ParamLocationUndefined,
Explode: false,
Required: false,
})
assert.NoError(t, err)
assert.Equal(t, *expectedBig, dstBigNumber)
}