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

Return consistent result using '[]' and accessor methods of Stripe::StripeObject class #1338

Open
cleve-fauna opened this issue Feb 21, 2024 · 1 comment

Comments

@cleve-fauna
Copy link

cleve-fauna commented Feb 21, 2024

Is your feature request related to a problem? Please describe.

Presently, using [] or an accessor on a StripeObject return different results:

 irb(main):005> require('stripe')
 => true
 irb(main):006> x = Stripe::StripeObject.new()
 => 
#<Stripe::StripeObject:0x13088> JSON: {
...
 irb(main):007> x.a
 /Users/cleve/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/stripe-5.55.0/lib/stripe/stripe_object.rb:399:in `method_missing': undefined method `a' for #<Stripe::StripeObject:0x13088> JSON: { (NoMethodError)
}
        from (irb):7:in `<main>'
        from /Users/cleve/.asdf/installs/ruby/3.1.4/lib/ruby/gems/3.1.0/gems/irb-1.9.0/exe/irb:9:in `<top (required)>'
        from /Users/cleve/.asdf/installs/ruby/3.1.4/bin/irb:25:in `load'
        from /Users/cleve/.asdf/installs/ruby/3.1.4/bin/irb:25:in `<main>'
 irb(main):008> x["a"]
 => nil
 irb(main):009> 

Using the accessor raises a NoMethodError while the use of the [] method returns nil.

Describe the solution you'd like

Have both accessor patterns generate the same output for non-existent fields. This would be more consistent semantically.

Describe alternatives you've considered

It is easy to work around this using [] or the accessor depending on the context.

Additional context

In my case I am using the metadata field of stripe payment intent to control some operational metrics and backend processes. Hence - we need to check metadata fields to determine what action, if any, to take and what metrics to report. That is when this difference was encountered.

I could not find documentation for the fact that the accessor raises errors while the hash does not - short of updating the library documenting the difference in the Stripe Ruby docs would be a nice win.

@pakrym-stripe
Copy link
Contributor

I've documented the behavior in #1339. I'll keep the issue open to see if we get other reports of inconsistency being confusing.

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

No branches or pull requests

2 participants