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
Added the Admin API path each resource's operations live at
Damian Legawiec43d752187dd1
diff --git a/spree/api/lib/spree/api/agent_resource_map.rb b/spree/api/lib/spree/api/agent_resource_map.rbindex 7590c12f78..a14acc591a 100644--- a/spree/api/lib/spree/api/agent_resource_map.rb+++ b/spree/api/lib/spree/api/agent_resource_map.rb@@ -148,6 +148,39 @@ module Spree## @param controller [Class]# @return [Hash, nil]+ # Where this controller's operations live, read from the routes rather+ # than guessed from the key: a nested or renamed resource keeps its+ # real path, and a controller with no route is not reachable at all.+ #+ # The shallowest route wins. A resource reached both at the top level+ # and under a parent (`/products` and+ # `/catalogs/:catalog_id/products`) is addressed by the former: a tool+ # given a product id has no parent id to fill in, and the nested route+ # would leave `:catalog_id` in the path.+ #+ # @return [Hash{Symbol => String}] action => path template+ # Across colliding controllers, the shallowest path per action.+ def shallowest_paths(candidates)+ candidates.flat_map { |entry| Array(entry[:api_paths]).to_a }.+ group_by(&:first).+ transform_values { |pairs| pairs.map(&:last).min_by { |path| [path.count(':'), path.length] } }+ end++ def api_paths_for(controller)+ candidates = Spree::Core::Engine.routes.routes.filter_map do |route|+ next unless route.defaults[:controller] == controller.controller_path++ action = route.defaults[:action]&.to_sym+ next if action.nil?++ [action, route.path.spec.to_s.sub('(.:format)', '')]+ end++ candidates.group_by(&:first).transform_values do |pairs|+ pairs.map(&:last).min_by { |path| [path.count(':'), path.length] }+ end+ end+def derive(controller)instance = controller.allocatemodel = safely(instance, :model_class)@@ -172,6 +205,7 @@ module Spreepermission: "read_#{scope}",serializer_name: serializer.name,dashboard_path: DASHBOARD_PATHS[key],+ api_paths: api_paths_for(controller),write_permission: write_permission_for(model, scope, create_workflow, update_workflow, writable, controller),writable_attributes: writable,create_workflow_key: create_workflow,@@ -225,7 +259,13 @@ module Spree# One controller refusing DISTINCT is enough: its sort would# make Postgres reject the query, and whichever controller# happened to sort first is not an answer.- distinct: candidates.all? { |entry| entry[:distinct] }+ distinct: candidates.all? { |entry| entry[:distinct] },+ # The shallowest route across every controller claiming this+ # key. `narrowest` is chosen by permission breadth, which says+ # nothing about depth — so a resource served both at the top+ # level and under a parent kept whichever won on scope, and a+ # tool was handed a path with an unfillable `:parent_id` in it.+ api_paths: shallowest_paths(candidates))endenddiff --git a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rbindex 143ee1b11b..ba0fb6cd54 100644--- a/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb+++ b/spree/api/spec/lib/spree/agent_tools/resource_map_spec.rb@@ -65,6 +65,34 @@ RSpec.describe Spree::AgentTools::ResourceMap do# Postgres refuses SELECT DISTINCT beside an ORDER BY the select list does# not carry, which is why some controllers turn it off. A search adding it# anyway answered every price query with a 500.+ # A tool names an operation, so the map has to know where each resource's+ # operations live. Read from the routes rather than built from the key.+ describe 'the Admin API path for a resource' do+ it 'is the top-level route, not a nested one' do+ expect(described_class.find('products').api_path(:index)).to eq('/api/v3/admin/products')+ expect(described_class.find('products').api_path(:show)).to eq('/api/v3/admin/products/:id')+ end++ # Products are served by five controllers, including+ # `catalogs/products`. Collisions resolve by permission breadth, which+ # says nothing about depth — so the winner kept whichever path won on+ # scope, and a tool was handed `:catalog_id` with nothing to fill it.+ it 'prefers the shallowest path across colliding controllers' do+ %w[products orders variants].each do |key|+ expect(described_class.find(key).api_path(:index)).not_to include(':')+ end+ end++ it 'keeps the parent segment for a genuinely nested resource' do+ expect(described_class.find('line_items').api_path(:index)).to include(':')+ end++ it 'knows which resources a tool can dispatch to' do+ expect(described_class.find('products')).to be_dispatchable+ expect(described_class.find('imports')).not_to be_dispatchable+ end+ end+describe 'a resource whose controller disables DISTINCT' doit 'carries that through to the map' doexpect(described_class.find('prices').distinct?).to be(false)diff --git a/spree/core/lib/spree/agent_tools/resource_map.rb b/spree/core/lib/spree/agent_tools/resource_map.rbindex 2210f1fbb5..00abe00842 100644--- a/spree/core/lib/spree/agent_tools/resource_map.rb+++ b/spree/core/lib/spree/agent_tools/resource_map.rb@@ -19,7 +19,21 @@ module SpreeEntry = Struct.new(:key, :model_name, :permission, :serializer_name,:dashboard_path, :scope_name, :write_permission,:writable_attributes, :create_workflow_key, :update_workflow_key,- :distinct, keyword_init: true) do+ :distinct, :api_paths, keyword_init: true) do+ # The Admin API path for one of this resource's operations, which is+ # how a tool reaches it.+ #+ # @param action [Symbol] :index, :show, :create, :update, :destroy+ # @return [String, nil]+ def api_path(action)+ Array(api_paths).to_h[action.to_sym]+ end++ # @return [Boolean] whether a tool can dispatch to this resource+ def dispatchable?+ api_path(:index).present?+ end+# Whether a search may add DISTINCT.## Postgres rejects SELECT DISTINCT beside an ORDER BY the select list@@ -174,14 +188,15 @@ module Spree# @return [Entry]def register(key:, model_name:, permission:, serializer_name:, dashboard_path: nil, scope_name: nil,write_permission: nil, writable_attributes: [], create_workflow_key: nil,- update_workflow_key: nil, distinct: nil)+ update_workflow_key: nil, distinct: nil, api_paths: nil)entry = Entry.new(key: key.to_s, model_name: model_name, permission: permission,serializer_name: serializer_name, dashboard_path: dashboard_path,scope_name: scope_name, write_permission: write_permission,writable_attributes: writable_attributes,create_workflow_key: create_workflow_key,update_workflow_key: update_workflow_key,- distinct: distinct)+ distinct: distinct,+ api_paths: api_paths&.to_a)entries[entry.key] = entryend