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

make optional event attributes optional #1936

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ type EventData struct {
// Object containing the API resource relevant to the event. For example, an `invoice.created` event will have a full [invoice object](https://stripe.com/docs/api#invoice_object) as the value of the object key.
Object map[string]interface{} `json:"-"`
// Object containing the names of the updated attributes and their values prior to the event (only included in events of type `*.updated`). If an array attribute has any updated elements, this object contains the entire array. In Stripe API versions 2017-04-06 or earlier, an updated array attribute in this object includes only the updated array elements.
PreviousAttributes map[string]interface{} `json:"previous_attributes"`
PreviousAttributes map[string]interface{} `json:"previous_attributes,omitempty"`
Raw json.RawMessage `json:"object"`
}

Expand Down Expand Up @@ -348,7 +348,7 @@ type EventRequest struct {
type Event struct {
APIResource
// The connected account that originates the event.
Account string `json:"account"`
Account string `json:"account,omitempty"`
// The Stripe API version used to render `data`. This property is populated only for events on or after October 31, 2014.
APIVersion string `json:"api_version"`
// Time at which the object was created. Measured in seconds since the Unix epoch.
Expand Down
42 changes: 42 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package stripe

import (
"encoding/json"
"testing"

assert "github.com/stretchr/testify/require"
Expand Down Expand Up @@ -53,3 +54,44 @@ func TestGetObjectValue(t *testing.T) {
event.GetObjectValue("top_level_key", "bad_key")
})
}

func TestEmptyOptionalFields(t *testing.T) {

// Marshals from a JSON object - without optional attributes
{
v := Event{}
data, err := json.Marshal(&v)
assert.NoError(t, err)

expected := `{"api_version":"","created":0,"data":null,"id":"","livemode":false,"object":"","pending_webhooks":0,"request":null,"type":""}`
assert.Equal(t, expected, string(data))
}

// Marshals from a JSON object - with empty optional attributes
{
v := Event{Account: "", Data: &EventData{PreviousAttributes: map[string]interface{}{}}}
data, err := json.Marshal(&v)
assert.NoError(t, err)

expected := `{"api_version":"","created":0,"data":{"object":null},"id":"","livemode":false,"object":"","pending_webhooks":0,"request":null,"type":""}`
assert.Equal(t, expected, string(data))
}

// Marshals from a JSON object - with filled account attribute
{
v := Event{
Account: "",
Data: &EventData{
PreviousAttributes: map[string]interface{}{
"amount_paid": 0,
},
},
}
data, err := json.Marshal(&v)
assert.NoError(t, err)

expected := `{"api_version":"","created":0,"data":{"previous_attributes":{"amount_paid":0},"object":null},"id":"","livemode":false,"object":"","pending_webhooks":0,"request":null,"type":""}`
assert.Equal(t, expected, string(data))
}

}