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.

VEN-339 Webhook payload by default should also include all top-level includes of the resource (#11614)

Aleksandar Petrushev377b8cb5565b
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index 8f5e3c15fc..3b2e2c4796 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -32,16 +32,27 @@ module Spree
private
def webhook_payload_body
- resource_serializer.new(self).serializable_hash.to_json
+ resource_serializer.new(self, include: included_relationships).serializable_hash.to_json
end
def inferred_event_name(operation)
"#{self.class.name.demodulize.tableize.singularize}.#{operation}"
end
+ def included_relationships
+ if resource_serializer.relationships_to_serialize
+ resource_serializer.relationships_to_serialize.keys
+ else
+ []
+ end
+ end
+
def resource_serializer
- demodulized_class_name = self.class.to_s.demodulize
- "Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".constantize
+ @resource_serializer ||=
+ begin
+ demodulized_class_name = self.class.to_s.demodulize
+ "Spree::Api::V2::Platform::#{demodulized_class_name}Serializer".constantize
+ end
end
def updating_only_timestamps?
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index f35e9f70d6..7fe74735e2 100644
--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
@@ -46,7 +46,12 @@ describe Spree::Webhooks::HasWebhooks do
end
context 'without DISABLE_SPREE_WEBHOOKS' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::ProductSerializer.new(
+ product,
+ include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
context 'after_create_commit' do
let(:event_name) { 'product.create' }
@@ -66,7 +71,12 @@ describe Spree::Webhooks::HasWebhooks do
end
context 'with a class name with multiple words' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::CmsPageSerializer.new(cms_page).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::CmsPageSerializer.new(
+ cms_page,
+ include: Spree::Api::V2::Platform::CmsPageSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }
let(:event_name) { 'cms_page.create' }
@@ -118,7 +128,12 @@ describe Spree::Webhooks::HasWebhooks do
context 'on touch events from callbacks' do
let!(:store2) { create(:store) }
let!(:cms_page) { create(:cms_homepage, store: store2, locale: 'en') }
- let(:body) { Spree::Api::V2::Platform::StoreSerializer.new(store2).serializable_hash }
+ let(:body) do
+ Spree::Api::V2::Platform::StoreSerializer.new(
+ store2,
+ include: Spree::Api::V2::Platform::StoreSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
before { store2.changes_applied }
diff --git a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
index a15abc991a..3590b320b1 100644
--- a/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/order_decorator_spec.rb
@@ -2,7 +2,12 @@ require 'spec_helper'
describe Spree::Api::Webhooks::OrderDecorator do
let(:store) { create(:store, default: true) }
- let(:webhook_payload_body) { Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::OrderSerializer.new(
+ order,
+ include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
describe 'order.canceled' do
describe 'completed -> canceled' do
diff --git a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
index 4427bbe361..85ce2fd984 100644
--- a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb
@@ -1,10 +1,19 @@
require 'spec_helper'
describe Spree::Api::Webhooks::PaymentDecorator do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::PaymentSerializer.new(payment).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::PaymentSerializer.new(
+ payment,
+ include: Spree::Api::V2::Platform::PaymentSerializer.relationships_to_serialize.keys - [:state_changes]
+ ).serializable_hash
+ end
let(:payment) { create(:payment) }
- before { allow(payment).to receive_message_chain(:state_changes, :create!) }
+ before do
+ allow(payment).to receive_message_chain(:state_changes, :create!)
+ # because it state_changes is an instance of Double and can not be serialized
+ allow(payment).to receive(:included_relationships).and_return(Spree::Api::V2::Platform::PaymentSerializer.relationships_to_serialize.keys - [:state_changes])
+ end
describe 'payment.paid' do
let(:event_name) { 'payment.paid' }
@@ -57,7 +66,12 @@ describe Spree::Api::Webhooks::PaymentDecorator do
describe 'order.paid' do
subject { Timecop.freeze { another_payment.complete } }
- let(:webhook_payload_body) { Spree::Api::V2::Platform::OrderSerializer.new(order).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::OrderSerializer.new(
+ order,
+ include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:order) { payment.order }
let(:event_name) { 'order.paid' }
let!(:another_payment) { create(:payment, order: order) }
diff --git a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
index 9b2f42a603..73fdc62ba8 100644
--- a/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/product_decorator_spec.rb
@@ -4,7 +4,12 @@ describe Spree::Api::Webhooks::ProductDecorator do
let(:product) { create(:product) }
context 'emitting product.discontinued' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::ProductSerializer.new(
+ product,
+ include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:event_name) { 'product.discontinued' }
diff --git a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
index ff60fa5bff..bd0272a53e 100644
--- a/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/shipment_decorator_spec.rb
@@ -10,10 +10,18 @@ describe Spree::Api::Webhooks::ShipmentDecorator do
# because after_ship queues the HTTP request before finishing the transition, hence
# the total state changes that are sent in the body is one less.
allow(shipment).to receive_message_chain(:state_changes, :create!)
+
+ # because state_changes is an instance of Double and can not be serialized
+ allow_any_instance_of(Spree::Shipment).to receive(:included_relationships).and_return(Spree::Api::V2::Platform::ShipmentSerializer.relationships_to_serialize.keys - [:state_changes])
end
context 'emitting shipment.shipped' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::ShipmentSerializer.new(shipment).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::ShipmentSerializer.new(
+ shipment,
+ include: Spree::Api::V2::Platform::ShipmentSerializer.relationships_to_serialize.keys - [:state_changes]
+ ).serializable_hash
+ end
let(:event_name) { 'shipment.shipped' }
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
@@ -37,7 +45,14 @@ describe Spree::Api::Webhooks::ShipmentDecorator do
end
context 'emitting order.shipped' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::OrderSerializer.new(order.reload).serializable_hash }
+ let(:webhook_payload_body) do
+ webhook_payload_body = Spree::Api::V2::Platform::OrderSerializer.new(
+ order.reload,
+ include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ webhook_payload_body[:included].each { |resource_hash| resource_hash[:relationships][:state_changes][:data] = [] if resource_hash[:type] == :shipment }
+ webhook_payload_body
+ end
let(:event_name) { 'order.shipped' }
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let!(:shipments) do
@@ -60,9 +75,12 @@ describe Spree::Api::Webhooks::ShipmentDecorator do
before do
shipments[0].ship
shipments[1].ready
+ shipments.each { |s| s.state_changes.destroy_all }
end
- it { expect { shipments[1].ship }.to emit_webhook_event(event_name) }
+ it do
+ expect { shipments[1].ship }.to emit_webhook_event(event_name)
+ end
end
context 'without all order shipments shipped' do
@@ -77,6 +95,7 @@ describe Spree::Api::Webhooks::ShipmentDecorator do
before do
shipments[0].ship
shipments[1].cancel
+ shipments.each { |s| s.state_changes.destroy_all }
end
it { expect { shipments[1].ship }.to emit_webhook_event(event_name) }
diff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
index 07607c0d6e..0bbbfd76a3 100644
--- a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
@@ -1,7 +1,12 @@
require 'spec_helper'
describe Spree::Api::Webhooks::StockItemDecorator do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::VariantSerializer.new(
+ variant,
+ include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:variant) { create(:variant) }
let(:stock_item) { variant.stock_items.first }
let(:stock_location) { variant.stock_locations.first }
@@ -9,7 +14,12 @@ describe Spree::Api::Webhooks::StockItemDecorator do
describe 'emitting product.backorderable' do
subject { stock_item.update(backorderable: backorderable) }
- let(:webhook_payload_body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::ProductSerializer.new(
+ product,
+ include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:product) { variant.product }
let(:event_name) { 'product.backorderable' }
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
diff --git a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
index 1ede4b1acc..23d906b6a3 100644
--- a/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_movement_decorator_spec.rb
@@ -1,7 +1,12 @@
require 'spec_helper'
describe Spree::Api::Webhooks::StockMovementDecorator do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::VariantSerializer.new(variant.reload).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::VariantSerializer.new(
+ variant.reload,
+ include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:stock_item) { create(:stock_item) }
let(:stock_location) { variant.stock_locations.first }
@@ -10,7 +15,12 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
let!(:product) { create(:product, stores: [store]) }
let!(:variant) { create(:variant, product: product) }
let!(:variant2) { create(:variant, product: product) }
- let(:webhook_payload_body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::ProductSerializer.new(
+ product,
+ include: Spree::Api::V2::Platform::ProductSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
describe 'emitting product.out_of_stock' do
let(:event_name) { 'product.out_of_stock' }
@@ -212,13 +222,17 @@ describe Spree::Api::Webhooks::StockMovementDecorator do
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let!(:variant) { stock_item.variant }
- before { Spree::StockItem.update_all(backorderable: false) }
+ before do
+ Spree::StockItem.update_all(backorderable: false)
+ end
describe 'when the variant goes out of stock' do
let(:movement_quantity) { -stock_item.count_on_hand }
it 'emits the variant.out_of_stock event' do
- expect { subject }.to emit_webhook_event(event_name)
+ ActiveRecord::Base.no_touching do
+ expect { subject }.to emit_webhook_event(event_name)
+ end
end
end
diff --git a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
index 497f8368e2..04161a80ff 100644
--- a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb
@@ -4,7 +4,12 @@ describe Spree::Api::Webhooks::VariantDecorator do
let(:variant) { create(:variant) }
context 'emitting variant.discontinued' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::VariantSerializer.new(
+ variant,
+ include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:event_name) { 'variant.discontinued' }
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
diff --git a/api/spec/services/spree/webhooks/subscribers/handle_request_spec.rb b/api/spec/services/spree/webhooks/subscribers/handle_request_spec.rb
index 03ae8f5f4d..be38c99239 100644
--- a/api/spec/services/spree/webhooks/subscribers/handle_request_spec.rb
+++ b/api/spec/services/spree/webhooks/subscribers/handle_request_spec.rb
@@ -11,7 +11,10 @@ describe Spree::Webhooks::Subscribers::HandleRequest do
end
let(:webhook_payload_body) do
- Spree::Api::V2::Platform::AddressSerializer.new(resource).serializable_hash.to_json
+ Spree::Api::V2::Platform::AddressSerializer.new(
+ resource,
+ include: Spree::Api::V2::Platform::AddressSerializer.relationships_to_serialize.keys
+ ).serializable_hash.to_json
end
let(:event_name) { 'order.canceled' }
let(:event) { Spree::Webhooks::Event.find_by(name: event_name, subscriber_id: subscriber.id, url: url) }
@@ -134,7 +137,12 @@ describe Spree::Webhooks::Subscribers::HandleRequest do
end
context 'full flow' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::OrderSerializer.new(order.reload).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::OrderSerializer.new(
+ order.reload,
+ include: Spree::Api::V2::Platform::OrderSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:event) { Spree::Webhooks::Event.find_by(name: event_name, subscriber_id: subscriber.id, url: url) }
let(:event_name) { 'order.placed' }
let(:order) { create(:order, email: '[email protected]') }
diff --git a/api/spec/services/spree/webhooks_spec.rb b/api/spec/services/spree/webhooks_spec.rb
index 7d5b428f31..3a9f4be8d6 100644
--- a/api/spec/services/spree/webhooks_spec.rb
+++ b/api/spec/services/spree/webhooks_spec.rb
@@ -2,7 +2,12 @@ require 'spec_helper'
describe Spree::Webhooks do
describe '#disable_webhooks' do
- let(:webhook_payload_body) { Spree::Api::V2::Platform::VariantSerializer.new(variant).serializable_hash }
+ let(:webhook_payload_body) do
+ Spree::Api::V2::Platform::VariantSerializer.new(
+ variant,
+ include: Spree::Api::V2::Platform::VariantSerializer.relationships_to_serialize.keys
+ ).serializable_hash
+ end
let(:event_name) { 'variant.discontinued' }
let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }
let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }