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

Ignore read-only attributes when updating through REST resources #1157

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
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