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
fix(api): scope nested category products to manual + collection PLP locale fallback
Łukasz Adamczykf38fe241758c
diff --git a/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rbindex db9d2e7767..b67949d46e 100644--- a/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/categories/products_controller.rb@@ -70,8 +70,12 @@ module Spreeend# Loads the parent category (runs before the base set_resource).+ # Scoped to +manual+ so automatic (rule-based) rows — hidden by the+ # categories CRUD controller and migrating to Spree::Collection — can't+ # have their membership listed or mutated through this endpoint.def set_parent- @category = Spree::Category.accessible_by(current_ability, :update).+ @category = Spree::Category.manual.+ accessible_by(current_ability, :update).for_store(current_store).find_by_prefix_id!(params[:category_id])authorize_parent!(@category)diff --git a/spree/api/app/controllers/spree/api/v3/store/collections/products_controller.rb b/spree/api/app/controllers/spree/api/v3/store/collections/products_controller.rbindex 622e0b518f..5063600ce3 100644--- a/spree/api/app/controllers/spree/api/v3/store/collections/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/store/collections/products_controller.rb@@ -38,14 +38,16 @@ module Spreedirection == 'desc' ? "-#{field}" : fieldend- # Resolve by permalink or prefixed ID, mirroring CollectionsController#find_resource.+ # Resolve by permalink or prefixed ID, mirroring CollectionsController#find_resource+ # (including the default-locale fallback so a non-default-locale permalink still+ # resolves the PLP the collection detail page can load).def current_collection@current_collection ||= beginid = params[:collection_id]if id.to_s.start_with?('coll_')current_store.collections.find_by_prefix_id!(id)else- current_store.collections.i18n.find_by!(permalink: id)+ find_with_fallback_default_locale { current_store.collections.i18n.find_by!(permalink: id) }endendenddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rbindex 5483e4ca41..a2275d853c 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/categories/products_controller_spec.rb@@ -48,6 +48,25 @@ RSpec.describe Spree::Api::V3::Admin::Categories::ProductsController, type: :conendend+ # Automatic (rule-based) rows are hidden by the categories CRUD controller and+ # migrate to Spree::Collection; membership can't be read or mutated here either.+ describe 'automatic (rule-based) categories' do+ let!(:automatic_category) { Spree::Category.create!(name: 'On Sale', store: store, automatic: true) }+ let!(:product) { create(:product, stores: [store]) }++ it 'are not listable through the nested products endpoint' do+ get :index, params: { category_id: automatic_category.prefixed_id }, as: :json+ expect(response).to have_http_status(:not_found)+ end++ it 'do not accept product membership' do+ post :create, params: { category_id: automatic_category.prefixed_id, product_id: product.prefixed_id }, as: :json++ expect(response).to have_http_status(:not_found)+ expect(Spree::ProductCategory.where(category_id: automatic_category.id)).to be_empty+ end+ end+describe 'POST #create' dolet!(:product) { create(:product, stores: [store]) }diff --git a/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rbindex 967c68059f..9878bf65b6 100644--- a/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb@@ -36,5 +36,23 @@ RSpec.describe Spree::Api::V3::Store::Collections::ProductsController, type: :coexpect(response).to have_http_status(:not_found)end++ # Mirrors Store::CollectionsController#find_resource: a permalink that only+ # exists in the default locale must still resolve the PLP on a non-default+ # locale, so the detail page and its product listing stay in sync.+ context 'locale fallback', if: Spree::Collection.include?(Spree::TranslatableResource) do+ before do+ allow(store).to receive(:supported_locales_list).and_return(%w[en fr])+ allow(store).to receive(:default_locale).and_return('en')+ end++ it 'resolves the default-locale permalink on a non-default locale' do+ request.headers['x-spree-locale'] = 'fr'+ get :index, params: { collection_id: collection.permalink }++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].pluck('id')).to include(in_product.prefixed_id)+ end+ endendend