Skip to content

Commit

Permalink
Fix CustomerAddress.all returning no results
Browse files Browse the repository at this point in the history
Previously, when fetching `all` on CustomerAddress it would return an
empty array. Two issues (Shopify#970, Shopify#1018) were opened outlining the problem.

Initial attempt was to override `json_response_body_name` which worked
for fetching arrays of results. It doesn't work for fetching individual
results though.

I wound up overriding `create_instances_from_response` for
CustomerAddress. It isn't ideal but it does seem to behave correctly.

Assertions have been added to the the tests as well.
  • Loading branch information
cdmwebs committed Oct 25, 2023
1 parent 72750ae commit 301cbf7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/shopify_api/rest/resources/2023_07/customer_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,10 @@ def json_body_name()
end

sig do
returns(T::Array[String])
returns(String)
end
def json_response_body_names()
[
"customer_address",
"address"
]
def json_response_body_name()
"addresses"
end

sig do
Expand Down Expand Up @@ -207,5 +204,24 @@ def set(
)
end

class << self
sig { params(response: Clients::HttpResponse, session: Auth::Session).returns(T::Array[ShopifyAPI::Rest::Base]) }
def create_instances_from_response(response:, session:)
objects = []

body = T.cast(response.body, T::Hash[String, T.untyped])

if body.key?('customer_addresses') || body.key?('addresses')
key = body.key?('customer_addresses') ? 'customer_addresses' : 'addresses'
body[key].each do |entry|
objects << create_instance(data: entry, session: session)
end
elsif body.key?('customer_address')
objects << create_instance(data: body['customer_address'], session: session)
end

objects
end
end
end
end
40 changes: 40 additions & 0 deletions test/rest/2023_07/customer_address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ def test_1()
)

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2023-07/customers/207119551/addresses.json?limit=1")
assert_equal(1, response.count)

response = response.first if response.respond_to?(:first)

# Assert attributes are correctly typed preventing Sorbet errors downstream
if response.respond_to?(:original_state)
response&.original_state&.each do |key, value|
next if key == :default

begin
response.send(key)
rescue TypeError => error
fail TypeError.new("#{self.class}##{key} is mistyped: #{error.message}")
end
response.send(key)
end
end
end

sig do
Expand All @@ -64,6 +81,26 @@ def test_2()
)

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2023-07/customers/207119551/addresses.json")
<<<<<<< HEAD
=======
assert_equal(1, response.count)

response = response.first if response.respond_to?(:first)

# Assert attributes are correctly typed preventing Sorbet errors downstream
if response.respond_to?(:original_state)
response&.original_state&.each do |key, value|
next if key == :default

begin
response.send(key)
rescue TypeError => error
fail TypeError.new("#{self.class}##{key} is mistyped: #{error.message}")
end
response.send(key)
end
end
>>>>>>> 3528ed8 (Fix CustomerAddress.all returning no results)
end

sig do
Expand All @@ -83,12 +120,15 @@ def test_3()
)

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2023-07/customers/207119551/addresses/207119551.json")
assert_equal(207119551, response.id)

response = response.first if response.respond_to?(:first)

# Assert attributes are correctly typed preventing Sorbet errors downstream
if response.respond_to?(:original_state)
response&.original_state&.each do |key, value|
next if key == :default

begin
response.send(key)
rescue TypeError => error
Expand Down

0 comments on commit 301cbf7

Please sign in to comment.