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

Cannot delete all addons from the subscriptions #213

Open
kudrykv opened this issue Apr 22, 2024 · 1 comment
Open

Cannot delete all addons from the subscriptions #213

kudrykv opened this issue Apr 22, 2024 · 1 comment

Comments

@kudrykv
Copy link

kudrykv commented Apr 22, 2024

Describe the bug

There is a subscription with a few addons. The intention is to delete all addons on renewal. To perform this action, this curl suffices:

 curl \
  -u 'apikey:' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/vnd.recurly.v2021-02-25+json' \
  -X POST \
  -d '{"timeframe": "renewal", "add_ons": []}' \
  'https://v3.recurly.com/subscriptions/uuid-my-uuid/change'

However, there is an issue with the SubscriptionChangeCreate type, AddOns field. Because it is annotated with omitempty (json:"add_ons,omitempty"), the field is not being marshalled even if the array is allocated.

To Reproduce

package play_test

import (
	"os"
	"testing"

	"github.com/recurly/recurly-client-go/v4"
)

func TestRecurlyRemoveLastAddon(t *testing.T) {
	t.Parallel()

	client, err := recurly.NewClient(os.Getenv("RECURLY_API_KEY"))
	if err != nil {
		t.Fatal(err)
	}

	t.Run("remove last addon, initialized empty addon list", func(t *testing.T) {
		t.Parallel()

		renewal := "renewal"
		changeCreate := &recurly.SubscriptionChangeCreate{
			Timeframe: &renewal,
			AddOns:    make([]recurly.SubscriptionAddOnUpdate, 0),
		}

		_, err = client.CreateSubscriptionChange("uuid-70647702d223609410b7a948528e1dad", changeCreate)
		if err != nil {
			t.Fatal(err)
		}
	})
}


func TestSubscriptionChangeCreateMarshal(t *testing.T) {
	t.Parallel()

	item := recurly.SubscriptionChangeCreate{
		AddOns:    make([]recurly.SubscriptionAddOnUpdate, 0),
	}

	jsonBytes, err := json.Marshal(item)
	if err != nil {
		t.Fatal(err)
	}

	if string(jsonBytes) != `{"timeframe":"renewal","add_ons":[]}` {
		t.Fatalf("want != have: %s", string(jsonBytes))
	}
}

In the same time, simply removing omitempty would break the statement from the documentation: If this value is omitted your existing add-ons will be unaffected. By removing omitempty it'll always marshal the value, resulting in either an array of addons, an empty array (if allocated), or in null. Passing null on the endpoint raises Recurly error:

{
  "error": {
    "type": "validation",
    "message": "Please remove the following unpermitted parameters from your request: add_ons.",
    "params":[{"param":"add_ons"}]
  }
}

Expected behavior

Initialized slice being marshalled and addons being removed (on renewal in my case).

Your Environment

  • Which version of this library are you using?
    v4.48.0
  • Which version of golang are you using?
    1.21
@kudrykv kudrykv changed the title Cannot delete all addons from the subscriptions. Cannot delete all addons from the subscriptions Apr 22, 2024
@maciejjwojcik
Copy link

Hi, just wanted to bump this. I really need this functionality fixed ASAP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants