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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeprivatedef webhook_payload_body- resource_serializer.new(self).serializable_hash.to_json+ resource_serializer.new(self, include: included_relationships).serializable_hash.to_jsonenddef 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+ endenddef 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.rbindex 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 doendcontext '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+ endcontext 'after_create_commit' dolet(:event_name) { 'product.create' }@@ -66,7 +71,12 @@ describe Spree::Webhooks::HasWebhooks doendcontext '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+ endlet(:cms_page) { create(:cms_homepage, store: store, locale: 'en') }let(:event_name) { 'cms_page.create' }@@ -118,7 +128,12 @@ describe Spree::Webhooks::HasWebhooks docontext 'on touch events from callbacks' dolet!(: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+ endbefore { 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.rbindex 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 dolet(: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+ enddescribe 'order.canceled' dodescribe 'completed -> canceled' dodiff --git a/api/spec/models/spree/api/webhooks/payment_decorator_spec.rb b/api/spec/models/spree/api/webhooks/payment_decorator_spec.rbindex 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+ endlet(: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])+ enddescribe 'payment.paid' dolet(:event_name) { 'payment.paid' }@@ -57,7 +66,12 @@ describe Spree::Api::Webhooks::PaymentDecorator dodescribe 'order.paid' dosubject { 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+ endlet(: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.rbindex 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 dolet(: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+ endlet!(: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.rbindex 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])endcontext '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+ endlet(:event_name) { 'shipment.shipped' }let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }@@ -37,7 +45,14 @@ describe Spree::Api::Webhooks::ShipmentDecorator doendcontext '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+ endlet(: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 dobefore doshipments[0].shipshipments[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)+ endendcontext 'without all order shipments shipped' do@@ -77,6 +95,7 @@ describe Spree::Api::Webhooks::ShipmentDecorator dobefore doshipments[0].shipshipments[1].cancel+ shipments.each { |s| s.state_changes.destroy_all }endit { 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.rbindex 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+ endlet(: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 dodescribe 'emitting product.backorderable' dosubject { 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+ endlet(: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.rbindex 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+ endlet(:stock_item) { create(:stock_item) }let(:stock_location) { variant.stock_locations.first }@@ -10,7 +15,12 @@ describe Spree::Api::Webhooks::StockMovementDecorator dolet!(: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+ enddescribe 'emitting product.out_of_stock' dolet(:event_name) { 'product.out_of_stock' }@@ -212,13 +222,17 @@ describe Spree::Api::Webhooks::StockMovementDecorator dolet!(: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)+ enddescribe 'when the variant goes out of stock' dolet(: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)+ endendenddiff --git a/api/spec/models/spree/api/webhooks/variant_decorator_spec.rb b/api/spec/models/spree/api/webhooks/variant_decorator_spec.rbindex 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 dolet(: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+ endlet(: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.rbindex 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 doendlet(: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_jsonendlet(: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 doendcontext '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+ endlet(: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.rbindex 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 dodescribe '#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+ endlet(:event_name) { 'variant.discontinued' }let!(:webhook_subscriber) { create(:webhook_subscriber, :active, subscriptions: [event_name]) }let(:queue_requests) { instance_double(Spree::Webhooks::Subscribers::QueueRequests) }