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

Expanding a product when retrieving a plan causes JSON unmarshalling errors #692

Closed
majelbstoat opened this issue Sep 11, 2018 · 6 comments · Fixed by #693
Closed

Expanding a product when retrieving a plan causes JSON unmarshalling errors #692

majelbstoat opened this issue Sep 11, 2018 · 6 comments · Fixed by #693

Comments

@majelbstoat
Copy link
Contributor

Simple repro:

		p, err := plan.Get(code, &stripe.PlanParams{
			Params: stripe.Params{
				Expand: []*string{stripe.String("product")},
			},
		})
		if err != nil {
			log.Fatal(err)
		}

Expected outcome is that a plan is returned. Actual behaviour is:

2018/09/11 15:23:28 json: cannot unmarshal object into Go struct field Plan.product of type string

The following also fails for the same reason.

		params := &stripe.PlanParams{}
		params.AddExpand("product")
		p, err := plan.Get(code, params)

Current workaround is to just make a second request, but that's kind of wasteful.

I'd submit a PR, but I'm not sure what your preferred approach to supporting both string and expanded return types is.

@majelbstoat
Copy link
Contributor Author

(The Java SDK has Product for the string, and ProductObject for the object, which is kind of ugly, but works.)

@majelbstoat
Copy link
Contributor Author

Ah, I think it's missing an UnMarshalJSON method. I'll take a crack at it.

@remi-stripe
Copy link
Contributor

@majelbstoat Nice catch and sorry about the trouble. The solution is to replace the string by a *Product and it will "just work".

@majelbstoat
Copy link
Contributor Author

ooh, neat.

@majelbstoat
Copy link
Contributor Author

wait, no, I don't actually understand 😂. Expand takes a slice of string pointers: []*string. stripe.String() takes a string. I don't see anything that will take a Product pointer for expansion 😬. Can you show a quick example?

@remi-stripe
Copy link
Contributor

@majelbstoat Sorry I could have been clearer! This line says:

	Product         string              `json:"product"`

This means that when you deserialize a plan we expect Product to be a string. But if you expand it it will be a hash. So instead you should have this line:

	Product         *Product              `json:"product"`

This will let stripe-go deserialize the product properly. If a string is returned we will automatically set Product.Id to the string value. If it's a hash we will deserialize the product automatically.

This works because the Product struct has a custom UnmarshalJSON here which will deserialize the resource based on whether it's a string or a hash.

nadaismail-stripe pushed a commit that referenced this issue Oct 18, 2024
Bumps [sentry-ruby](https://github.com/getsentry/sentry-ruby) from 5.4.1 to 5.4.2.
- [Release notes](https://github.com/getsentry/sentry-ruby/releases)
- [Changelog](https://github.com/getsentry/sentry-ruby/blob/master/CHANGELOG.md)
- [Commits](getsentry/sentry-ruby@5.4.1...5.4.2)

---
updated-dependencies:
- dependency-name: sentry-ruby
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

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

Successfully merging a pull request may close this issue.

2 participants