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

Convert json_response_body_names to array #1208

Merged
merged 4 commits into from
Sep 5, 2023
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
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
- [1208](https://github.com/Shopify/shopify-api-ruby/pull/1208) Fix CustomerAddress and FulfillmentRequest methods

## 13.1.0

Expand Down
26 changes: 13 additions & 13 deletions lib/shopify_api/rest/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def json_body_name
class_name.underscore
end

sig { returns(String) }
def json_response_body_name
class_name
sig { returns(T::Array[String]) }
def json_response_body_names
[class_name]
end

sig { returns(T.nilable(String)) }
Expand Down Expand Up @@ -210,14 +210,16 @@ def create_instances_from_response(response:, session:)

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

response_name = json_response_body_name
response_names = json_response_body_names

if body.key?(response_name.pluralize) || (body.key?(response_name) && body[response_name].is_a?(Array))
(body[response_name.pluralize] || body[response_name]).each do |entry|
objects << create_instance(data: entry, session: session)
response_names.each do |response_name|
if body.key?(response_name.pluralize) || (body.key?(response_name) && body[response_name].is_a?(Array))
(body[response_name.pluralize] || body[response_name]).each do |entry|
objects << create_instance(data: entry, session: session)
end
elsif body.key?(response_name)
objects << create_instance(data: body[response_name], session: session)
end
elsif body.key?(response_name)
objects << create_instance(data: body[response_name], session: session)
end

objects
Expand Down Expand Up @@ -347,10 +349,8 @@ def save(update_object: false)
)

if update_object
self.class.create_instance(
data: response.body[self.class.class_name.downcase],
session: @session, instance: self
)
response_name = self.class.json_response_body_names & response.body.keys
self.class.create_instance(data: response.body[response_name.first], session: @session, instance: self)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any risk for response.body.keys not containing any of the json_response_body_names?

Copy link
Contributor Author

@mllemango mllemango Aug 31, 2023

Choose a reason for hiding this comment

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

there is, but no riskier than the previous code, sorbet would throw the nil error

end
rescue ShopifyAPI::Errors::HttpResponseError => e
@errors.errors << e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def initialize(session: ShopifyAPI::Context.active_session)

class << self
sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"fulfillment_order"
def json_response_body_names()
[
"fulfillment_order"
]
end

sig do
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2022_10/customer_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def json_body_name()
"address"
end

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

sig do
params(
id: T.any(Integer, String),
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2022_10/fulfillment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def initialize(session: ShopifyAPI::Context.active_session)
attr_reader :fulfillment_order_id

class << self
sig do
returns(T::Array[String])
end
def json_response_body_names()
[
"submitted_fulfillment_order",
"fulfillment_order"
]
end

end

sig do
Expand Down
8 changes: 5 additions & 3 deletions lib/shopify_api/rest/resources/2022_10/order_risk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def json_body_name()
end

sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"risk"
def json_response_body_names()
[
"risk"
]
end

sig do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def initialize(session: ShopifyAPI::Context.active_session)

class << self
sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"fulfillment_order"
def json_response_body_names()
[
"fulfillment_order"
]
end

sig do
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2023_01/customer_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def json_body_name()
"address"
end

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

sig do
params(
id: T.any(Integer, String),
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2023_01/fulfillment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def initialize(session: ShopifyAPI::Context.active_session)
attr_reader :fulfillment_order_id

class << self
sig do
returns(T::Array[String])
end
def json_response_body_names()
[
"submitted_fulfillment_order",
"fulfillment_order"
]
end

end

sig do
Expand Down
8 changes: 5 additions & 3 deletions lib/shopify_api/rest/resources/2023_01/order_risk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def json_body_name()
end

sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"risk"
def json_response_body_names()
[
"risk"
]
end

sig do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def initialize(session: ShopifyAPI::Context.active_session)

class << self
sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"fulfillment_order"
def json_response_body_names()
[
"fulfillment_order"
]
end

sig do
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2023_04/customer_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ def json_body_name()
"address"
end

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

sig do
params(
id: T.any(Integer, String),
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2023_04/fulfillment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def initialize(session: ShopifyAPI::Context.active_session)
attr_reader :fulfillment_order_id

class << self
sig do
returns(T::Array[String])
end
def json_response_body_names()
[
"submitted_fulfillment_order",
"fulfillment_order"
]
end

end

sig do
Expand Down
8 changes: 5 additions & 3 deletions lib/shopify_api/rest/resources/2023_04/order_risk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def json_body_name()
end

sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"risk"
def json_response_body_names()
[
"risk"
]
end

sig do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def initialize(session: ShopifyAPI::Context.active_session)

class << self
sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"fulfillment_order"
def json_response_body_names()
[
"fulfillment_order"
]
end

sig do
Expand Down
10 changes: 10 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,16 @@ def json_body_name()
"address"
end

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

sig do
params(
id: T.any(Integer, String),
Expand Down
10 changes: 10 additions & 0 deletions lib/shopify_api/rest/resources/2023_07/fulfillment_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def initialize(session: ShopifyAPI::Context.active_session)
attr_reader :fulfillment_order_id

class << self
sig do
returns(T::Array[String])
end
def json_response_body_names()
[
"submitted_fulfillment_order",
"fulfillment_order"
]
end

end

sig do
Expand Down
8 changes: 5 additions & 3 deletions lib/shopify_api/rest/resources/2023_07/order_risk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def json_body_name()
end

sig do
returns(String)
returns(T::Array[String])
end
def json_response_body_name()
"risk"
def json_response_body_names()
[
"risk"
]
end

sig do
Expand Down
17 changes: 17 additions & 0 deletions test/clients/base_rest_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ def test_to_hash_for_saving_excludes_unsaveable_attributes
refute_includes(resource.to_hash(true), "unsaveable_attribute")
end

def test_save_allows_custom_json_body_names
request_body = { fake_resource: { attribute: "attribute" } }.to_json
response_body = { fake_resource_response: { id: 1, attribute: "attribute updated" } }.to_json

stub_request(:post, "#{@prefix}/fake_resources.json")
.with(body: request_body)
.to_return(status: 201, body: response_body)

resource = TestHelpers::FakeResource.new(session: @session)
resource.attribute = "attribute"
TestHelpers::FakeResource.stubs(:json_response_body_names).returns(["fake_resource_response"])

resource.save!
assert_equal(1, resource.id)
assert_equal("attribute updated", resource.attribute)
end

def test_deletes_existing_resource_and_fails_on_deleting_nonexistent_resource
resource = TestHelpers::FakeResource.new(session: @session)
resource.id = 1
Expand Down
Loading