From 7e6f5bd650f78ec159fb49cb3d211e2f268fb906 Mon Sep 17 00:00:00 2001 From: Nelson Date: Wed, 23 Aug 2023 15:56:24 -0400 Subject: [PATCH] Patch bug with newly added attributes after resource creation (#1197) * handle no key * fix bug with change detection for associations * Update test/clients/base_rest_resource_test.rb Co-authored-by: Zoey Lan <102243935+zzooeeyy@users.noreply.github.com> --------- Co-authored-by: Zoey Lan <102243935+zzooeeyy@users.noreply.github.com> --- lib/shopify_api/rest/base.rb | 10 +++++++++- test/clients/base_rest_resource_test.rb | 26 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/shopify_api/rest/base.rb b/lib/shopify_api/rest/base.rb index 650439c7e..14f549082 100644 --- a/lib/shopify_api/rest/base.rb +++ b/lib/shopify_api/rest/base.rb @@ -365,10 +365,18 @@ def attributes_to_update self.class.read_only_attributes&.include?("@#{attribute}".to_sym) end - HashDiff::Comparison.new( + diff = HashDiff::Comparison.new( deep_stringify_keys(original_state_for_update), deep_stringify_keys(to_hash(true)), ).left_diff + + diff.each do |attribute, value| + if value.is_a?(Hash) && value[0] == HashDiff::NO_VALUE + diff[attribute] = send(attribute) + end + end + + diff end sig { returns(Symbol) } diff --git a/test/clients/base_rest_resource_test.rb b/test/clients/base_rest_resource_test.rb index 5094090de..43469b7e3 100644 --- a/test/clients/base_rest_resource_test.rb +++ b/test/clients/base_rest_resource_test.rb @@ -203,6 +203,32 @@ def test_loads_unknown_attribute assert_equal("some-value", resource.to_hash["unknown"]) end + def test_saves_removing_children + draft_order_data = { + "id" => 1124601987358, + "line_items" => [{ + "id" => 58266522976542, + "title" => "The Minimal Snowboard", + "price" => "885.95", + }], + } + draft_order = ShopifyAPI::DraftOrder.create_instance( + session: @session, + data: draft_order_data, + ) + + body = draft_order_data.dup + body["line_items"] = [] + stubbed_request = stub_request(:put, "#{@prefix}/draft_orders/#{draft_order_data.dig("id")}.json") + .with(body: hash_including("draft_order": { line_items: [] })) + .to_return(status: 200) + + draft_order.line_items = [] + draft_order.save + + assert_requested(stubbed_request) + end + def test_loads_unknown_attribute_with_special_character body = { fake_resource: { id: 1, attribute: "attribute", "unknown?": "some-value" } }.to_json