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

Pass requestor to all deserialized objects including lists #1507

Merged
merged 2 commits into from
Dec 17, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def raw_request(method, url, base_address: :api, params: {}, opts: {})

def deserialize(data, api_mode: :v1)
data = JSON.parse(data) if data.is_a?(String)
Util.convert_to_stripe_object(data, {}, api_mode: api_mode)
Util.convert_to_stripe_object(data, {}, api_mode: api_mode, requestor: @requestor)
end
end
end
6 changes: 3 additions & 3 deletions lib/stripe/stripe_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def inspect
def update_attributes(values, opts = {}, dirty: true)
values.each do |k, v|
add_accessors([k], values) unless metaclass.method_defined?(k.to_sym)
@values[k] = Util.convert_to_stripe_object(v, opts, api_mode: @api_mode)
@values[k] = Util.convert_to_stripe_object(v, opts, api_mode: @api_mode, requestor: @requestor)
dirty_value!(@values[k]) if dirty
@unsaved_values.add(k)
end
Expand Down Expand Up @@ -361,7 +361,7 @@ class << self; self; end
"We interpret empty strings as nil in requests. " \
"You may set (object).#{k} = nil to delete the property."
end
@values[k] = Util.convert_to_stripe_object(v, @opts, api_mode: @api_mode)
@values[k] = Util.convert_to_stripe_object(v, @opts, api_mode: @api_mode, requestor: @requestor)
dirty_value!(@values[k])
@unsaved_values.add(k)
end
Expand Down Expand Up @@ -534,7 +534,7 @@ class << self; self; end
# example by appending a new hash onto `additional_owners` for an
# account.
elsif value.is_a?(Hash)
Util.convert_to_stripe_object(value, @opts).serialize_params
Util.convert_to_stripe_object(value, @opts, api_mode: @api_mode, requestor: @requestor).serialize_params

elsif value.is_a?(StripeObject)
update = value.serialize_params(force: force)
Expand Down
2 changes: 1 addition & 1 deletion lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def self.convert_to_stripe_object_with_params(

case data
when Array
data.map { |i| convert_to_stripe_object(i, opts, api_mode: api_mode) }
data.map { |i| convert_to_stripe_object(i, opts, api_mode: api_mode, requestor: requestor) }
when Hash
# TODO: This is a terrible hack.
# Waiting on https://jira.corp.stripe.com/browse/API_SERVICES-3167 to add
Expand Down
7 changes: 7 additions & 0 deletions test/stripe/list_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class ListObjectTest < Test::Unit::TestCase
assert_equal 1, list.count
end

should "be able to refresh objects in list" do
list = Stripe::Customer.list
assert_not_nil list.first.instance_variable_get(:@requestor)
cus = list.first.refresh
assert cus.is_a?(Stripe::Customer)
end

should "provide #each" do
arr = [
{ id: 1 },
Expand Down
7 changes: 7 additions & 0 deletions test/stripe/search_result_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class SearchResultObjectTest < Test::Unit::TestCase
assert_equal 1, list.count
end

should "be able to refresh objects in search" do
list = Stripe::Customer.search({ query: "name:'fakename'" })
assert_not_nil list.first.instance_variable_get(:@requestor)
cus = list.first.refresh
assert cus.is_a?(Stripe::Customer)
end

should "provide #each" do
arr = [
{ id: 1 },
Expand Down
13 changes: 13 additions & 0 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ class StripeClientTest < Test::Unit::TestCase
assert_equal obj.id, "acc_123"
end

should "allow refresh on deserialized object" do
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"

stub_request(:get, "#{Stripe::DEFAULT_API_BASE}/v1/accounts/acc_123")
.to_return(status: 200, body: expected_body)

obj = @client.deserialize(expected_body)
obj = obj.refresh

assert_equal obj.class, Stripe::Account
assert_equal obj.id, "acc_123"
end

should "deserializes hash into unknown object" do
expected_body = { "id" => "acc_123", "object" => "unknown" }

Expand Down
Loading