Skip to content

Commit

Permalink
Ignore read-only attributes when updating through REST resources (#1157)
Browse files Browse the repository at this point in the history
* Add test for saving resources with read only attributes

* Fixing save logic when the resource has read-only attributes

* Adding changelog entry
  • Loading branch information
AnthonyRobertson17 committed May 18, 2023
1 parent 9e3008d commit fca16f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased

- [#1157](https://github.com/Shopify/shopify-api-ruby/pull/1157) Fix an issue where read-only attributes are included when saving REST resources

## 13.0.0

- [#1140](https://github.com/Shopify/shopify-api-ruby/pull/1140) ⚠️ [Breaking] Reformat Http error messages to be JSON parsable.
Expand Down
6 changes: 5 additions & 1 deletion lib/shopify_api/rest/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,12 @@ def save(update_object: false)

sig { returns(T::Hash[String, String]) }
def attributes_to_update
original_state_for_update = original_state.reject do |attribute, _|
self.class.read_only_attributes&.include?("@#{attribute}".to_sym)
end

HashDiff::Comparison.new(
deep_stringify_keys(original_state),
deep_stringify_keys(original_state_for_update),
deep_stringify_keys(to_hash(true)),
).left_diff
end
Expand Down
47 changes: 47 additions & 0 deletions test/clients/base_rest_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,53 @@ def test_put_request_for_has_one_associaiton_works

customer.save
end

def test_put_requests_for_resource_with_read_only_attributes
stub_request(:get, "https://test-shop.myshopify.com/admin/api/unstable/variants/169.json")
.to_return(
status: 200,
body: JSON.generate(
{
"variant" => {
"id" => 169,
"product_id" => 116,
"title" => "Default Title",
"price" => "2.50",
"sku" => "SKU123",
"position" => 1,
"inventory_policy" => "deny",
"compare_at_price" => nil,
"fulfillment_service" => "manual",
"inventory_management" => nil,
"option1" => "Default Title",
"option2" => nil,
"option3" => nil,
"created_at" => "2023-05-10T16:37:23-04:00",
"updated_at" => "2023-05-10T16:37:23-04:00",
"taxable" => true,
"barcode" => "0000",
"grams" => 45359236093,
"image_id" => nil,
"weight" => 99999998.0,
"weight_unit" => "lb",
"inventory_item_id" => 167,
"inventory_quantity" => 0,
"old_inventory_quantity" => 0,
"requires_shipping" => true,
"admin_graphql_api_id" => "gid://shopify/ProductVariant/169",
},
},
),
)

variant = ShopifyAPI::Variant.find(id: 169, session: @session)
variant.client.expects(:put).with(
body: { "variant" => { "barcode" => "1234" } },
path: "variants/169.json",
)
variant.barcode = "1234"
variant.save
end
end
end
end

0 comments on commit fca16f1

Please sign in to comment.