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

Stripe events have the object property serialized twice #797

Closed
sudhirj opened this issue Feb 23, 2019 · 3 comments
Closed

Stripe events have the object property serialized twice #797

sudhirj opened this issue Feb 23, 2019 · 3 comments
Assignees

Comments

@sudhirj
Copy link

sudhirj commented Feb 23, 2019

I'm serializing all the events coming in for audit purposes, and the object property is being represented in all serializations twice.

In https://github.com/stripe/stripe-go/blob/master/event.go#L36

// EventData is the unmarshalled object as a map.
type EventData struct {
	Object             map[string]interface{}
	PreviousAttributes map[string]interface{} `json:"previous_attributes"`
	Raw                json.RawMessage        `json:"object"`
}

The Raw Go property is serialized once in the JSON as object, and the Object Go property is serialized again as Object in the JSON. Ideally, the Object property should have a json:"-" annotation.

@remi-stripe
Copy link
Contributor

@sudhirj Thanks a lot for the report! I opened a PR for this and we'll get this released likely next week!

@EVODelavega
Copy link
Contributor

EVODelavega commented Feb 26, 2019

@sudhirj A while back I started work on something to avoid having to manually map that map[string]interface{} type onto the correct object. I ended up hacking out something to generate code based on the docs. We're currently using that approach, but maybe a similar approach could be adopted in the stripe-go package as part of this change? I've included a link to gist in the last comment here: #729

Just add a cusom marshaller to the template/type check for nil pointers, and set the object field accordingly. Something like this would do:

// add `json:"-"` to all fields, or `json:"fieldname,omitempty"`
func (e Event) MarshalJSON() ([]byte, error) {
    type mEvent struct {
        stripe.Event // embed the original event
        Object interface{} `json:"object"`
    }
    me := mEvent{
        Event: e.Event, // set stripe.Event
     }
    if e.SKU != nil {
        me.Object = e.SKU
    } else if e.Order != nil {
        me.Object = e.Order
    } 
    // and so on
    return json.Marshal(me)
}

Just like the switch in the custom UnmarshalJSON func, you can generate the custom marshaller. Should be pretty straightforward.

So straightforward, in fact, that I've added the part to generate a custom marshaller to the template: https://gist.github.com/EVODelavega/288a109d8ed9e908c99175b75cfb70a8

I've got a generate rule in my Makefile to spit out the result:

go run generate/events.go -input generate/events.json -output internal/connector/quote/event_generated.go && gofmt -w internal/connector/quote/event_generated.go

@remi-stripe
Copy link
Contributor

@sudhirj Thanks for the patience here. We just released a fix in 57.4.0

nadaismail-stripe pushed a commit that referenced this issue Oct 18, 2024
Bumps [webdrivers](https://github.com/titusfortner/webdrivers) from 5.0.0 to 5.1.0.
- [Release notes](https://github.com/titusfortner/webdrivers/releases)
- [Changelog](https://github.com/titusfortner/webdrivers/blob/main/CHANGELOG.md)
- [Commits](titusfortner/webdrivers@v5.0.0...v5.1.0)

---
updated-dependencies:
- dependency-name: webdrivers
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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

3 participants