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 Aug 9, 2023
1 parent 77ab36c commit 3528ed8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/shopify_api/rest/resources/2023_07/customer_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def json_body_name()
"address"
end

sig do
returns(String)
end
def json_response_body_name()
"addresses"
end

sig do
params(
id: T.any(Integer, String),
Expand Down Expand Up @@ -197,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
9 changes: 9 additions & 0 deletions test/rest/2023_07/customer_address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ 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
Expand All @@ -78,12 +81,15 @@ def test_2()
)

assert_requested(:get, "https://test-shop.myshopify.io/admin/api/2023-07/customers/207119551/addresses.json")
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
Expand Down Expand Up @@ -111,12 +117,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 3528ed8

Please sign in to comment.