From 3528ed8402fb91b0daf52e137138ff492f7a791c Mon Sep 17 00:00:00 2001 From: Chris Moore Date: Tue, 8 Aug 2023 19:36:37 -0400 Subject: [PATCH] Fix CustomerAddress.all returning no results Previously, when fetching `all` on CustomerAddress it would return an empty array. Two issues (#970, #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. --- .../resources/2023_07/customer_address.rb | 26 +++++++++++++++++++ test/rest/2023_07/customer_address_test.rb | 9 +++++++ 2 files changed, 35 insertions(+) diff --git a/lib/shopify_api/rest/resources/2023_07/customer_address.rb b/lib/shopify_api/rest/resources/2023_07/customer_address.rb index 460237f0c..a50858903 100644 --- a/lib/shopify_api/rest/resources/2023_07/customer_address.rb +++ b/lib/shopify_api/rest/resources/2023_07/customer_address.rb @@ -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), @@ -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 diff --git a/test/rest/2023_07/customer_address_test.rb b/test/rest/2023_07/customer_address_test.rb index c2eb4c5f5..a5fa95078 100644 --- a/test/rest/2023_07/customer_address_test.rb +++ b/test/rest/2023_07/customer_address_test.rb @@ -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 @@ -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 @@ -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