diff --git a/README.rst b/README.rst index ea838d367..e88d8b979 100644 --- a/README.rst +++ b/README.rst @@ -334,7 +334,7 @@ params for ``create`` and ``update`` actions are shared between them. These params can be extracted with ``def_param_group`` and ``param_group`` keywords. -The definition is looked up in the scope of the controller. If the +The definition is looked up in the scope of the controller and its subclasses. If the group is defined in a different controller, it might be referenced by specifying the second argument. diff --git a/lib/apipie/application.rb b/lib/apipie/application.rb index adf0a91ca..b6ec490b0 100644 --- a/lib/apipie/application.rb +++ b/lib/apipie/application.rb @@ -143,6 +143,8 @@ def get_param_group(controller, name) key = "#{controller.name}##{name}" if @param_groups.has_key?(key) return @param_groups[key] + elsif controller.superclass != Object + get_param_group(controller.superclass, name) else raise "param group #{key} not defined" end diff --git a/spec/dummy/app/controllers/api/v1/architectures_controller.rb b/spec/dummy/app/controllers/api/v1/architectures_controller.rb index 69949d841..29881ddff 100644 --- a/spec/dummy/app/controllers/api/v1/architectures_controller.rb +++ b/spec/dummy/app/controllers/api/v1/architectures_controller.rb @@ -3,13 +3,19 @@ module V1 class ArchitecturesController < V1::BaseController resource_description { name 'Architectures' } api :GET, "/architectures/", "List all architectures." + param_group :pagination def index end api :GET, "/architectures/:id/", "Show an architecture." + param_group :identifier def show end + def_param_group :identifier do + param :identifier, Hash, 'A hex based string to identify the resource.' + end + def_param_group :timestamps do param :created_at, String param :updated_at, String diff --git a/spec/dummy/app/controllers/api/v1/base_controller.rb b/spec/dummy/app/controllers/api/v1/base_controller.rb index ada0065e8..9208109e2 100644 --- a/spec/dummy/app/controllers/api/v1/base_controller.rb +++ b/spec/dummy/app/controllers/api/v1/base_controller.rb @@ -6,6 +6,15 @@ class BaseController < Api::BaseController app_info 'Version 1.0 description' api_base_url '/api/v1' end + + def_param_group :pagination do + param :page, Integer, 'The page of the resource.' + param :items_per_page, Integer, 'The number of items per page.' + end + + def_param_group :identifier do + param :identifier, Integer, 'The identifier of the resource.' + end end end end diff --git a/spec/lib/param_group_spec.rb b/spec/lib/param_group_spec.rb index 9db23c3bd..1af757035 100644 --- a/spec/lib/param_group_spec.rb +++ b/spec/lib/param_group_spec.rb @@ -51,10 +51,19 @@ expect(Apipie["overridden_concern_resources#create"].params.has_key?(:user)).to eq(false) end -it "shouldn't replace name of a parameter defined in the controller" do + it "shouldn't replace name of a parameter defined in the controller" do expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:concern)).to eq(true) expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:user)).to eq(false) end + it 'should allow controllers to inherit param groups from their parents' do + expect(Apipie['1.0#architectures#index'].params.key?(:page)).to be true + expect(Apipie['1.0#architectures#index'].params.key?(:items_per_page)) + .to be true + end + + it 'should be able to override inherited param_groups' do + expect(Apipie['1.0#architectures#show'].params[:identifier].validator.expected_type).to eq 'hash' + end end