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.

Move channel resolution in API only to Store API

Damian Legawiec2e5c831de3f6
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/channel_resolution.rb b/spree/api/app/controllers/concerns/spree/api/v3/channel_resolution.rb
index d447602edb..4698d8d4ed 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/channel_resolution.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/channel_resolution.rb
@@ -6,8 +6,9 @@ module Spree
# read channel context without threading it through method args.
#
# Resolution order:
- # 1. +X-Spree-Channel+ header value matched against +channels.code+
- # scoped to the current store
+ # 1. +X-Spree-Channel+ header value matched against +channels.code+ —
+ # or, if it looks like a prefixed ID (+ch_…+), against +channels.id+
+ # — scoped to the current store
# 2. +current_store.default_channel+
#
# The concern is a no-op if no channel matches — callers fall back to
@@ -39,11 +40,19 @@ module Spree
end
def channel_from_header
- code = request.headers[CHANNEL_HEADER].presence
- return nil if code.blank?
+ value = request.headers[CHANNEL_HEADER].presence
+ return nil if value.blank?
return nil unless current_store
- current_store.channels.active.find_by(code: code)
+ scope = current_store.channels.active
+ # Accept either a merchant-meaningful +code+ ("pos", "wholesale") or
+ # the opaque prefixed ID — mirrors how Store API endpoints accept
+ # either slug or prefixed ID (e.g. +products/{slug-or-id}+).
+ if Spree::PrefixedId.prefixed_id?(value)
+ scope.find_by_prefix_id(value)
+ else
+ scope.find_by(code: value)
+ end
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/base_controller.rb b/spree/api/app/controllers/spree/api/v3/base_controller.rb
index c9a047b430..1056efd193 100644
--- a/spree/api/app/controllers/spree/api/v3/base_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/base_controller.rb
@@ -10,7 +10,6 @@ module Spree
include Spree::Core::ControllerHelpers::StrongParameters
include Spree::Core::ControllerHelpers::Store
include Spree::Api::V3::LocaleAndCurrency
- include Spree::Api::V3::ChannelResolution
include Spree::Api::V3::JwtAuthentication
include Spree::Api::V3::ApiKeyAuthentication
include Spree::Api::V3::ErrorHandler
diff --git a/spree/api/app/controllers/spree/api/v3/store/auth_controller.rb b/spree/api/app/controllers/spree/api/v3/store/auth_controller.rb
index 0eecf9b910..63a6e5cc9f 100644
--- a/spree/api/app/controllers/spree/api/v3/store/auth_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/auth_controller.rb
@@ -70,8 +70,7 @@ module Spree
# Accepts: { "refresh_token": "rt_xxx" }
# Revokes the submitted refresh token. The token itself is the
# credential — no access JWT is required, so clients with an expired
- # access token can still log out. Matches Saleor's `tokenRevoke` and
- # Shopify's `customerAccessTokenDelete` model.
+ # access token can still log out.
def logout
refresh_token_value = params[:refresh_token]
diff --git a/spree/api/app/controllers/spree/api/v3/store/base_controller.rb b/spree/api/app/controllers/spree/api/v3/store/base_controller.rb
index fd330eef7f..6f7745cf4c 100644
--- a/spree/api/app/controllers/spree/api/v3/store/base_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/base_controller.rb
@@ -3,6 +3,12 @@ module Spree
module V3
module Store
class BaseController < Spree::Api::V3::BaseController
+ # Channel resolution is a Store API concern — admin endpoints return
+ # data across all channels and filter via Ransack instead. Including
+ # this here keeps the +X-Spree-Channel+ header from accidentally
+ # narrowing admin queries.
+ include Spree::Api::V3::ChannelResolution
+
# Require publishable API key for all Store API requests
before_action :authenticate_api_key!
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb
index 188238daf9..0e179ac3ee 100644
--- a/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb
@@ -2,7 +2,12 @@ module Spree
module Api
module V3
module Store
+ # Mirrors Store::BaseController's concerns. Both classes anchor parallel
+ # inheritance branches (V3::BaseController vs V3::ResourceController);
+ # any concern added here MUST also be added to Store::BaseController.
class ResourceController < Spree::Api::V3::ResourceController
+ include Spree::Api::V3::ChannelResolution
+
# Require publishable API key for all Store API requests
before_action :authenticate_api_key!
end
diff --git a/spree/api/spec/controllers/concerns/spree/api/v3/channel_resolution_spec.rb b/spree/api/spec/controllers/concerns/spree/api/v3/channel_resolution_spec.rb
index 9fb3150175..d9bc6a0ed5 100644
--- a/spree/api/spec/controllers/concerns/spree/api/v3/channel_resolution_spec.rb
+++ b/spree/api/spec/controllers/concerns/spree/api/v3/channel_resolution_spec.rb
@@ -19,7 +19,7 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
controller.send(:current_channel)
end
- it 'resolves channel from X-Spree-Channel header' do
+ it 'resolves channel from X-Spree-Channel header by code' do
request.headers['x-spree-channel'] = 'pos'
get :index
@@ -27,6 +27,22 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(resolved_channel).to eq(pos_channel)
end
+ it 'resolves channel from X-Spree-Channel header by prefixed ID' do
+ request.headers['x-spree-channel'] = pos_channel.prefixed_id
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(resolved_channel).to eq(pos_channel)
+ end
+
+ it 'falls back to store default channel for an unknown prefixed ID' do
+ request.headers['x-spree-channel'] = 'ch_nonexistent'
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(resolved_channel).to eq(store.default_channel)
+ end
+
it 'falls back to store default channel when header is absent' do
get :index