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.

Decode prefixed IDs in bare q[id_in]/q[id_eq] predicates

Damian Legawiecbc6123c42529
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb b/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb
index 645ee8b648..12fdbe447b 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/store/search_provider_support.rb
@@ -14,7 +14,11 @@ module Spree
def search_filters
q = params[:q]&.to_unsafe_h || params[:q] || {}
q = q.to_h if q.respond_to?(:to_h) && !q.is_a?(Hash)
- q.except('search').presence
+ # Decode Stripe-style prefixed IDs in `*_id_in`/`id_eq`/etc. so
+ # SPA + storefront filters can pass `prod_…` keys; the search
+ # provider hands the filter hash straight to Ransack on the
+ # underlying scope, which expects raw integer IDs.
+ decode_prefixed_id_predicates(q.except('search')).presence
end
def search_provider
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 1220d56366..b63ea137f4 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
@@ -43,6 +43,10 @@ module Spree
return @collection if @collection.present?
filters = params[:q]&.to_unsafe_h || params[:q] || {}
+ # Decode Stripe-style prefixed IDs in `*_id_in`/`id_eq`/etc. so SPA
+ # filters can pass `prod_…` keys; the search provider expects raw
+ # IDs because it goes straight to Ransack on the underlying scope.
+ filters = decode_prefixed_id_predicates(filters)
# `q[search]` is the global text-search predicate; pass it through
# the provider's `query` arg so it invokes `Product.search` rather
# than being treated as a Ransack predicate (which gets stripped
diff --git a/spree/api/app/controllers/spree/api/v3/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
index f73a45a29b..b7071a90ae 100644
--- a/spree/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -185,7 +185,11 @@ module Spree
end
end
- RANSACK_ID_PREDICATE_RE = /_id(?:s)?(?:_(?:eq|not_eq|in|not_in|lt|lteq|gt|gteq))?\z/.freeze
+ # Matches both prefixed-FK predicates (`product_id_in`, `tax_category_id_eq`)
+ # and the bare-`id` predicates (`id_in`, `id_eq`) on the resource's
+ # primary key. Without the bare-id branch, `q[id_in][]=prod_x` would
+ # be passed to Ransack verbatim and never match any row.
+ RANSACK_ID_PREDICATE_RE = /(?:\A|_)id(?:s)?(?:_(?:eq|not_eq|in|not_in|lt|lteq|gt|gteq))?\z/.freeze
def ransack_id_predicate?(key)
RANSACK_ID_PREDICATE_RE.match?(key.to_s)
end
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 bbaebaae91..9a045b6729 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
@@ -47,6 +47,41 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
end
end
+ # Regression for SPA pickers (`<ResourceMultiAutocomplete>` hydration):
+ # the products controller bypasses `ransack_params` for its custom
+ # search-provider flow, so prefixed-ID decoding has to live inside
+ # `#collection`. Without it `q[id_in][]=prod_…` returns zero rows.
+ context 'with q[id_in] using prefixed IDs' do
+ let!(:other_product) { create(:product, stores: [store]) }
+ let!(:third_product) { create(:product, stores: [store]) }
+
+ it 'decodes prefixed IDs and returns matching rows' do
+ get :index,
+ params: { q: { id_in: [product.prefixed_id, third_product.prefixed_id] } },
+ as: :json
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to contain_exactly(product.prefixed_id, third_product.prefixed_id)
+ end
+
+ it 'still accepts raw integer IDs' do
+ get :index, params: { q: { id_in: [product.id] } }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to eq([product.prefixed_id])
+ end
+
+ it 'decodes with q[id_eq] too' do
+ get :index, params: { q: { id_eq: other_product.prefixed_id } }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to eq([other_product.prefixed_id])
+ end
+ end
+
context 'with q[search] (full-text search)' do
let!(:matching_product) { create(:product, name: 'Espresso Machine', stores: [store]) }
let!(:non_matching_product) { create(:product, name: 'Garden Hose', stores: [store]) }
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index c53422a984..c9435ad677 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -113,6 +113,37 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(json_response['data'].first['id']).to eq(product.prefixed_id)
end
+ # Regression for SPA pickers (`<ResourceMultiAutocomplete>` hydration):
+ # the store products controller routes filters through
+ # `SearchProviderSupport#search_filters`, which bypasses the base
+ # `ransack_params` decoder. Without explicit decoding there, hydration
+ # calls like `q[id_in][]=prod_…` return zero rows.
+ context 'with q[id_in] using prefixed IDs' do
+ it 'decodes prefixed IDs and returns matching rows' do
+ get :index, params: { q: { id_in: [product.prefixed_id, product2.prefixed_id] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to contain_exactly(product.prefixed_id, product2.prefixed_id)
+ end
+
+ it 'still accepts raw integer IDs' do
+ get :index, params: { q: { id_in: [product.id] } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to eq([product.prefixed_id])
+ end
+
+ it 'decodes with q[id_eq] too' do
+ get :index, params: { q: { id_eq: product2.prefixed_id } }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to eq([product2.prefixed_id])
+ end
+ end
+
context 'filtering by option values' do
let(:option_type) { create(:option_type, :color) }
let(:option_value_red) { create(:option_value, option_type: option_type, name: 'red', presentation: 'Red') }