Skip to content

Commit

Permalink
Merge upstream and update generated code for v466
Browse files Browse the repository at this point in the history
  • Loading branch information
stripe-openapi[bot] committed Aug 16, 2023
2 parents 53cb666 + 63dc0d9 commit 6cde8af
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 17 deletions.
25 changes: 16 additions & 9 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ type ListParams struct {
// key or query the state of the API.
Context context.Context `form:"-"`

EndingBefore *string `form:"ending_before"`
Expand []*string `form:"expand"`
Filters Filters `form:"*"`
Limit *int64 `form:"limit"`
EndingBefore *string `form:"ending_before"`
// Deprecated: Please use Expand in the surrounding struct instead.
Expand []*string `form:"expand"`
Filters Filters `form:"*"`
Limit *int64 `form:"limit"`

// Single specifies whether this is a single page iterator. By default,
// listing through an iterator will automatically grab additional pages as
Expand All @@ -128,7 +129,8 @@ type ListParams struct {
StripeAccount *string `form:"-"` // Passed as header
}

// AddExpand appends a new field to expand.
// AddExpand on the embedded ListParams struct is deprecated.
// Deprecated: please use AddExpand on the surrounding struct instead.
func (p *ListParams) AddExpand(f string) {
p.Expand = append(p.Expand, &f)
}
Expand Down Expand Up @@ -188,14 +190,17 @@ type Params struct {
// key or query the state of the API.
Context context.Context `form:"-"`

// Deprecated: please use Expand in the surrounding struct instead.
Expand []*string `form:"expand"`
Extra *ExtraValues `form:"*"`

// Headers may be used to provide extra header lines on the HTTP request.
Headers http.Header `form:"-"`

IdempotencyKey *string `form:"-"` // Passed as header
Metadata map[string]string `form:"metadata"`
IdempotencyKey *string `form:"-"` // Passed as header

// Deprecated: Please use Metadata in the surrounding struct instead.
Metadata map[string]string `form:"metadata"`

// StripeAccount may contain the ID of a connected account. By including
// this field, the request is made as if it originated from the connected
Expand All @@ -204,7 +209,8 @@ type Params struct {
StripeAccount *string `form:"-"` // Passed as header
}

// AddExpand appends a new field to expand.
// AddExpand on the Params embedded struct is deprecated.
// Deprecated: please use Expand in the surrounding struct instead.
func (p *Params) AddExpand(f string) {
p.Expand = append(p.Expand, &f)
}
Expand All @@ -218,7 +224,8 @@ func (p *Params) AddExtra(key, value string) {
p.Extra.Add(key, value)
}

// AddMetadata adds a new key-value pair to the Metadata.
// AddMetadata on the Params embedded struct is deprecated.
// Deprecated: please use .AddMetadata of the surrounding struct.
func (p *Params) AddMetadata(key, value string) {
if p.Metadata == nil {
p.Metadata = make(map[string]string)
Expand Down
10 changes: 6 additions & 4 deletions search_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ type SearchParams struct {
// key or query the state of the API.
Context context.Context `form:"-"`

Query string `form:"query"`
Limit *int64 `form:"limit"`
Page *string `form:"page"`
Query string `form:"query"`
Limit *int64 `form:"limit"`
Page *string `form:"page"`
// Deprecated: Please use Expand in the surrounding struct instead.
Expand []*string `form:"expand"`

// Single specifies whether this is a single page iterator. By default,
Expand All @@ -64,7 +65,8 @@ type SearchParams struct {
StripeAccount *string `form:"-"` // Passed as header
}

// AddExpand appends a new field to expand.
// AddExpand on the embedded SearchParams struct is deprecated
// Deprecated: please use .AddExpand of the surrounding struct instead.
func (p *SearchParams) AddExpand(f string) {
p.Expand = append(p.Expand, &f)
}
Expand Down
27 changes: 23 additions & 4 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ type BackendImplementation struct {
requestMetricsBuffer chan requestMetrics
}

func extractParams(params ParamsContainer) (*form.Values, *Params) {
func extractParams(params ParamsContainer) (*form.Values, *Params, error) {
var formValues *form.Values
var commonParams *Params

Expand All @@ -296,23 +296,42 @@ func extractParams(params ParamsContainer) (*form.Values, *Params) {

if reflectValue.Kind() == reflect.Ptr && !reflectValue.IsNil() {
commonParams = params.GetParams()

if !reflectValue.Elem().FieldByName("Metadata").IsZero() {
if commonParams.Metadata != nil {
return nil, nil, fmt.Errorf("You cannot specify both the (deprecated) .Params.Metadata and .Metadata in %s", reflectValue.Elem().Type().Name())
}
}

if !reflectValue.Elem().FieldByName("Expand").IsZero() {
if commonParams.Expand != nil {
return nil, nil, fmt.Errorf("You cannot specify both the (deprecated) .Params.Expand and .Expand in %s", reflectValue.Elem().Type().Name())
}
}

formValues = &form.Values{}
form.AppendTo(formValues, params)
}
}
return formValues, commonParams
return formValues, commonParams, nil
}

// Call is the Backend.Call implementation for invoking Stripe APIs.
func (s *BackendImplementation) Call(method, path, key string, params ParamsContainer, v LastResponseSetter) error {
body, commonParams := extractParams(params)
body, commonParams, err := extractParams(params)
if err != nil {
return err
}
return s.CallRaw(method, path, key, body, commonParams, v)
}

// CallStreaming is the Backend.Call implementation for invoking Stripe APIs
// without buffering the response into memory.
func (s *BackendImplementation) CallStreaming(method, path, key string, params ParamsContainer, v StreamingLastResponseSetter) error {
formValues, commonParams := extractParams(params)
formValues, commonParams, err := extractParams(params)
if err != nil {
return err
}

var body string
if formValues != nil && !formValues.Empty() {
Expand Down
54 changes: 54 additions & 0 deletions stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,60 @@ func TestStripeAccount(t *testing.T) {
assert.Equal(t, "acct_123", req.Header.Get("Stripe-Account"))
}

func TestErrorOnDuplicateMetadata(t *testing.T) {
c := GetBackend(APIBackend).(*BackendImplementation)
type myParams struct {
Params `form:"*"`
Metadata map[string]string `form:"metadata"`
}

metadata := map[string]string{"foo": "bar"}
resource := APIResource{}
err := c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{}, &resource)
assert.NoError(t, err)

err =
c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{Metadata: metadata}, &resource)
assert.NoError(t, err)

err =
c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{Params: Params{Metadata: metadata}}, &resource)
assert.NoError(t, err)

err =
c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{Metadata: metadata, Params: Params{Metadata: metadata}}, &resource)
assert.Errorf(t, err, "You cannot specify both the (deprecated) .Params.Metadata and .Metadata in myParams")
}

func TestErrorOnDuplicateExpand(t *testing.T) {
c := GetBackend(APIBackend).(*BackendImplementation)
type myParams struct {
Params `form:"*"`
Expand []*string `form:"expand"`
}

expand := []*string{String("foo"), String("bar")}
resource := APIResource{}
err := c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{}, &resource)
assert.NoError(t, err)

err =
c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{Expand: expand}, &resource)
assert.NoError(t, err)

err =
c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{
Params: Params{Expand: expand},
}, &resource)
assert.NoError(t, err)

err =
c.Call("POST", "/v1/customers", "sk_test_xyz", &myParams{
Expand: expand, Params: Params{Expand: expand}}, &resource)

assert.Errorf(t, err, "You cannot specify both the (deprecated) .Params.Expand and .Expand in myParams")
}

func TestUnmarshalJSONVerbose(t *testing.T) {
type testServerResponse struct {
Message string `json:"message"`
Expand Down

0 comments on commit 6cde8af

Please sign in to comment.