Skip to content

Commit cad5e76

Browse files
Remove expanded instance variables after a resource has been updated (stripe#1698)
* Fix object-as-hash issue * Switch to removing member variables * Clarify test case
1 parent f08f685 commit cad5e76

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

lib/stripe/stripe_object.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ class << self; self; end
313313
protected def remove_accessors(keys)
314314
# not available in the #instance_eval below
315315
protected_fields = self.class.protected_fields
316+
obj = self # capture self to use inside instance_eval
316317

317318
metaclass.instance_eval do
318319
keys.each do |k|
@@ -342,6 +343,11 @@ class << self; self; end
342343
"collide with an API property name.")
343344
end
344345
end
346+
347+
# Also remove instance variables so that static attr_readers (if any exist)
348+
# will return nil instead of stale data or data of the wrong type
349+
ivar_name = :"@#{k}"
350+
obj.remove_instance_variable(ivar_name) if obj.instance_variable_defined?(ivar_name)
345351
end
346352
end
347353
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require File.expand_path("../test_helper", __dir__)
4+
5+
module Stripe
6+
class InvoicePaymentsTest < Test::Unit::TestCase
7+
context "invoice.payments field" do
8+
should "return nil after instance method calls when field not in response" do
9+
# Simulate invoice.retrieve() with payments field expanded
10+
stub_request(:get, "#{Stripe.api_base}/v1/invoices/in_123")
11+
.to_return(body: JSON.generate({
12+
id: "in_123",
13+
object: "invoice",
14+
amount_paid: 0,
15+
payments: {
16+
object: "list",
17+
data: [{
18+
id: "invpay_123",
19+
object: "invoice.payment",
20+
status: "open",
21+
}],
22+
has_more: false,
23+
url: "/v1/invoices/in_123/payments",
24+
},
25+
}))
26+
27+
# Simulate invoice.pay(), which returns invoice WITHOUT payments field
28+
stub_request(:post, "#{Stripe.api_base}/v1/invoices/in_123/pay")
29+
.to_return(body: JSON.generate({
30+
id: "in_123",
31+
object: "invoice",
32+
amount_paid: 0,
33+
status: "paid",
34+
# NOTE: payments field is NOT included in response
35+
}))
36+
37+
invoice = Stripe::Invoice.retrieve({ id: "in_123", expand: ["payments"] })
38+
39+
assert_not_nil invoice.payments, "payments should be non-nil before pay"
40+
invoice.pay
41+
assert_nil invoice.payments, "payments should be nil after pay when not in response"
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)