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.

SD-1439-Invalid-SQL-statement-when-filtering-Products-via-Storefront-API (#11298)

Konrad Gorazd5a8755e4daf4
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index 67e6d9c630..ed18c3eb0b 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -528,6 +528,19 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
end
+
+ context 'sorting by both price and sku' do
+ let(:skus_array) { json_response['data'].map { |p| p['attributes']['sku'] } }
+
+ before { get "/api/v2/storefront/products?sort=-sku,price" }
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it do
+ expect(skus_array).to eq(skus_array.sort.reverse)
+ expect(json_response['data'].count).to eq(store.products.available.count)
+ end
+ end
end
context 'paginate products' do
@@ -650,6 +663,32 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
end
+
+ # Regression test for SD-1439 ambiguous column name: count_on_hand
+ context 'with multiple params' do
+ before do
+ get '/api/v2/storefront/products?filter[backorderable]=true'\
+ '&filter[ids]=130'\
+ '&filter[in_stock]=true'\
+ '&filter[name]=rails'\
+ '&filter[options][tshirt-color]=Red'\
+ '&filter[price]=10,100'\
+ '&filter[properties][brand-name]=alpha'\
+ '&filter[purchasable]=true'\
+ '&filter[show_deleted]=true'\
+ '&filter[show_discontinued]=true'\
+ '&filter[skus]=SKU-123,SKU-345'\
+ '&filter[taxons]=1,2,3,4,5,6,7,8,9,10,11'\
+ '&image_transformation[quality]=anim consequat fugiat sed'\
+ '&image_transformation[size]=100x50'\
+ '&include=default_variant,variants,option_types,product_properties,taxons,images,primary_variant'\
+ '&page=1'\
+ '&per_page=25'\
+ '&sort=-updated_at,price,-name,created_at,-available_on,sku'
+ end
+
+ it_behaves_like 'returns 200 HTTP status'
+ end
end
describe 'products#show' do
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index dcdb8d8fc5..1765dc9366 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -60,7 +60,7 @@ module Spree
after_touch :clear_in_stock_cache
- scope :in_stock, -> { joins(:stock_items).where('count_on_hand > ? OR track_inventory = ?', 0, false) }
+ scope :in_stock, -> { joins(:stock_items).where("#{Spree::StockItem.table_name}.count_on_hand > ? OR #{Spree::Variant.table_name}.track_inventory = ?", 0, false) }
scope :backorderable, -> { joins(:stock_items).where(spree_stock_items: { backorderable: true }) }
scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }
diff --git a/core/app/sorters/spree/products/sort.rb b/core/app/sorters/spree/products/sort.rb
index d480faa860..dab39cbc0e 100644
--- a/core/app/sorters/spree/products/sort.rb
+++ b/core/app/sorters/spree/products/sort.rb
@@ -31,8 +31,14 @@ module Spree
def by_sku(scope)
return scope unless (value = sort_by?('sku'))
+ select_product_attributes = if scope.to_sql.include?("#{Spree::Product.table_name}.*")
+ ''
+ else
+ "#{Spree::Product.table_name}.*, "
+ end
+
scope.joins(:master).
- select("#{Spree::Product.table_name}.*, #{Spree::Variant.table_name}.sku").
+ select("#{select_product_attributes}#{Spree::Variant.table_name}.sku").
where(Spree::Variant.table_name.to_s => { is_master: true }).
order("#{Spree::Variant.table_name}.sku #{value[1]}")
end
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 4ff6de1630..cc9c835a1e 100644
--- a/core/spec/models/spree/product/scopes_spec.rb
+++ b/core/spec/models/spree/product/scopes_spec.rb
@@ -318,4 +318,11 @@ describe 'Product scopes', type: :model do
expect(products_by_store).to contain_exactly(product)
end
end
+
+ # Regression test for SD-1439 ambiguous column name: count_on_hand
+ describe '#in_stock.in_stock_or_backorderable' do
+ it do
+ expect { Spree::Product.in_stock.in_stock_or_backorderable.count }.not_to raise_error
+ end
+ end
end