Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Permit the id that makes inline digital asset updates idempotent

Damian Legawiecc8ca03f18ff1
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index 589cdedcf9..80d2dffd7e 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -249,11 +249,13 @@ module Spree
# importers rather than to a request body.
media: [*Spree::Media::WRITABLE_ATTRIBUTES, :id, :signed_id, :source_media_id, { variant_ids: [] }],
# Inline digital files, so a new product ships its downloadable
- # files in the same request. Create-only: entries carry a
+ # files in the same request. Additive: an entry carries a
# `signed_id` (uploaded file) or a `provider_type` (provider-backed
- # asset). Edits and deletes use the nested digital_assets endpoints.
- # See `Spree::Product#digital_assets=`.
- digital_assets: [:signed_id, :variant_id, :provider_type, :authorized_clicks, :authorized_days, provider_settings: {}],
+ # asset), and an `id` patches that asset in place rather than
+ # building a duplicate — so replaying a product's list on update
+ # does not accumulate copies. Removal uses the nested
+ # digital_assets endpoint. See `Spree::Product#digital_assets=`.
+ digital_assets: [:id, :signed_id, :variant_id, :provider_type, :authorized_clicks, :authorized_days, provider_settings: {}],
product_publications: [:id, :channel_id, :published_at, :unpublished_at],
variants: [
:id, :sku, :barcode,
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
index ec47870c81..c598c67942 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
@@ -1011,6 +1011,20 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(product.reload.categories).to include(category1)
expect(product.reload.categories).not_to include(foreign_category)
end
+
+ it 'patches an inline digital asset by id without duplicating it' do
+ asset = create(:digital_asset, variant: product.default_variant, authorized_clicks: 2)
+
+ expect {
+ patch :update, params: {
+ id: product.prefixed_id,
+ digital_assets: [{ id: asset.prefixed_id, authorized_clicks: 9 }]
+ }, as: :json
+ }.not_to change(Spree::DigitalAsset, :count)
+
+ expect(response).to have_http_status(:ok)
+ expect(asset.reload.authorized_clicks).to eq(9)
+ end
end
context 'with tags' do