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
Reject an invalid provider_type cleanly instead of 500ing
Damian Legawiecc9cd5a71e9dd
diff --git a/spree/core/app/models/spree/digital_asset.rb b/spree/core/app/models/spree/digital_asset.rbindex a90bd32e70..e82afeb0d9 100644--- a/spree/core/app/models/spree/digital_asset.rb+++ b/spree/core/app/models/spree/digital_asset.rb@@ -22,8 +22,11 @@ module Spree# and provider-backed assets (which carry no file) are exempt.DEFAULT_PROVIDER = 'Spree::DigitalAssetProvider::File'.freeze- validates :attachment, attached: true, if: -> { provider_class.requires_attachment? }validate :provider_type_is_registered, if: -> { provider_type.present? }+ # Only ask a valid provider whether it needs a file — an unregistered+ # provider_type is already rejected above, and calling requires_attachment?+ # on a class that is not a provider would 500 instead.+ validates :attachment, attached: true, if: -> { provider_registered? && provider_class.requires_attachment? }validates :authorized_clicks, numericality: { only_integer: true, greater_than: 0 }, allow_nil: truevalidates :authorized_days, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true@@ -105,11 +108,18 @@ module Spree# A provider_type must name a registered provider — a stale class name from# a removed gem, or a typo, is rejected rather than blowing up at download.def provider_type_is_registered- return if Spree.digital_asset_providers.map(&:to_s).include?(provider_type)+ return if provider_registered?errors.add(:provider_type, :not_registered)end+ # A blank provider_type is the File default (always registered); otherwise+ # it must name a registered provider. Guards the attachment validation so it+ # never calls requires_attachment? on a class that is not a provider.+ def provider_registered?+ provider_type.blank? || Spree.digital_asset_providers.map(&:to_s).include?(provider_type)+ end+# The legacy digital.* events are dual-emitted for one release# (webhook contract bridge — removed in 6.1).def publish_create_eventdiff --git a/spree/core/spec/models/spree/digital_asset_spec.rb b/spree/core/spec/models/spree/digital_asset_spec.rbindex c9f47adef9..e9436de292 100644--- a/spree/core/spec/models/spree/digital_asset_spec.rb+++ b/spree/core/spec/models/spree/digital_asset_spec.rb@@ -61,6 +61,17 @@ describe Spree::DigitalAsset, type: :model doexpect(asset.errors[:provider_type]).to be_presentend+ # A provider_type naming a real class that is not a provider must fail+ # validation cleanly rather than raising when the attachment validation+ # asks it whether it requires a file.+ it 'rejects a provider_type that names a non-provider class without raising' do+ asset = described_class.new(variant: variant, provider_type: 'Spree::Product')++ expect { asset.valid? }.not_to raise_error+ expect(asset).not_to be_valid+ expect(asset.errors[:provider_type]).to be_present+ end+it 'stores provider settings under one key in metadata' doasset = described_class.newasset.provider_settings = { 'pool_name' => 'winter' }