Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for adding new params via update_api #642

Merged
merged 2 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def apipie_update_params(methods, *args, &block)
params_ordered = dsl_data[:params].map do |args|
Apipie::ParamDescription.from_dsl_data(method_description, args)
end
ParamDescription.unify(method_description.params_ordered_self + params_ordered)
ParamDescription.merge(method_description.params_ordered_self, params_ordered)
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ def self.unify(params)
end.sort_by { |param| ordering.index(param.name) }
end

def self.merge(target_params, source_params)
params_to_merge, params_to_add = source_params.partition do |source_param|
target_params.any? { |target_param| source_param.name == target_param.name }
end
unify(target_params + params_to_merge)
target_params.concat(params_to_add)
end

# action awareness is being inherited from ancestors (in terms of
# nested params)
def action_aware?
Expand Down
1 change: 1 addition & 0 deletions spec/controllers/extended_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe ExtendedController do

it 'should include params from both original controller and extending concern' do
expect(Apipie["extended#create"].params.keys).to eq [:oauth, :user, :admin]
user_param = Apipie["extended#create"].params[:user]
expect(user_param.validator.params_ordered.map(&:name)).to eq [:name, :password, :from_concern]
end
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/extended_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class ExtendedController < ApplicationController
end
def create
end

apipie_update_params([:create]) do
param :admin, :boolean
end
end
30 changes: 20 additions & 10 deletions spec/lib/swagger/swagger_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,28 @@ def swagger_param_by_name(param_name, path, method='get')
end

def deep_match?(actual, expected, breadcrumb=[])
num = 0
for pdesc in expected do
if pdesc.is_a? Symbol
return false unless fields_match?(actual.params_ordered[num], pdesc, breadcrumb)
elsif pdesc.is_a? Hash
return false unless fields_match?(actual.params_ordered[num], pdesc.keys[0], breadcrumb)
return false unless deep_match?(actual.params_ordered[num].validator, pdesc.values[0], breadcrumb + [pdesc.keys[0]])
pending_params = actual.params_ordered.dup
expected.each do |expected_param|
expected_param_name = expected_param.is_a?(Hash) ? expected_param.keys.first : expected_param
actual_param = pending_params.find { |param| param.name.to_s == expected_param_name.to_s }
unless actual_param
@fail_message = "Couldn't find #{expected_param_name.inspect} among #{pending_params.map(&:name)} in #{breadcrumb.join('.')}"
return false
else
pending_params.delete_if { |p| p.object_id == actual_param.object_id }
end
num+=1

return false unless fields_match?(actual_param, expected_param_name, breadcrumb)
if expected_param.is_a? Hash
return false unless deep_match?(actual_param.validator, expected_param.values[0], breadcrumb + [expected_param.keys.first])
end
end

unless pending_params.empty?
@fail_message = "Unexpected properties #{pending_params.map(&:name)} in #{breadcrumb.join('.')}"
return false
end
@fail_message = "expected property count#{breadcrumb == [] ? '' : ' of ' + (breadcrumb).join('.')} (#{actual.params_ordered.count}) to be #{num}"
actual.params_ordered.count == num
true
end

def fields_match?(param, expected_name, breadcrumb)
Expand Down