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

Fix CustomerAddress.all returning empty array #1192

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api
## Unreleased

- [#1183](https://github.com/Shopify/shopify-api-ruby/pull/1189) Added string array support for fields parameter in Webhook::Registry
- [#1192](https://github.com/Shopify/shopify-api-ruby/pull/1192) Fix no results when fetching CustomerAddress.all

## 13.1.0

Expand Down
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this, but I'm not sure how to better handle. default is both an attribute and a method.


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