Skip to content

Commit f108bb5

Browse files
committed
Support for adding new params via update_api
Before this patch, we only supported updating nested hashes. For example param :user, Hash do param :login, String end and param :user, Hash do param :oauth, String end has been merged properly. However when merging top-level params, such as param :login, String and param :oauth, String no changes were propagated.
1 parent f4a3991 commit f108bb5

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

lib/apipie/dsl_definition.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def apipie_update_params(methods, *args, &block)
498498
params_ordered = dsl_data[:params].map do |args|
499499
Apipie::ParamDescription.from_dsl_data(method_description, args)
500500
end
501-
ParamDescription.unify(method_description.params_ordered_self + params_ordered)
501+
ParamDescription.merge(method_description.params_ordered_self, params_ordered)
502502
end
503503
end
504504

lib/apipie/param_description.rb

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ def self.unify(params)
197197
end.sort_by { |param| ordering.index(param.name) }
198198
end
199199

200+
def self.merge(target_params, source_params)
201+
params_to_merge, params_to_add = source_params.partition do |source_param|
202+
target_params.any? { |target_param| source_param.name == target_param.name }
203+
end
204+
unify(target_params + params_to_merge)
205+
target_params.concat(params_to_add)
206+
end
207+
200208
# action awareness is being inherited from ancestors (in terms of
201209
# nested params)
202210
def action_aware?

spec/controllers/extended_controller_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
describe ExtendedController do
44

55
it 'should include params from both original controller and extending concern' do
6+
expect(Apipie["extended#create"].params.keys).to eq [:oauth, :user, :admin]
67
user_param = Apipie["extended#create"].params[:user]
78
expect(user_param.validator.params_ordered.map(&:name)).to eq [:name, :password, :from_concern]
89
end

spec/dummy/app/controllers/extended_controller.rb

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@ class ExtendedController < ApplicationController
77
end
88
def create
99
end
10+
11+
apipie_update_params([:create]) do
12+
param :admin, :boolean
13+
end
1014
end

0 commit comments

Comments
 (0)