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

Include sub params of nested hash in the documentation #230

Merged
merged 1 commit into from
Apr 2, 2014
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
37 changes: 12 additions & 25 deletions lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,32 +100,19 @@ def parents_and_self
end

def to_json
if validator.is_a? Apipie::Validator::HashValidator
{
:name => name.to_s,
:full_name => full_name,
:description => desc,
:required => required,
:allow_nil => allow_nil,
:validator => validator.to_s,
:expected_type => validator.expected_type,
:metadata => metadata,
:show => show,
:params => validator.hash_params_ordered.map(&:to_json)
}
else
{
:name => name.to_s,
:full_name => full_name,
:description => desc,
:required => required,
:allow_nil => allow_nil,
:validator => validator.to_s,
:metadata => metadata,
:show => show,
:expected_type => validator.expected_type
}
hash = { :name => name.to_s,
:full_name => full_name,
:description => desc,
:required => required,
:allow_nil => allow_nil,
:validator => validator.to_s,
:expected_type => validator.expected_type,
:metadata => metadata,
:show => show }
if sub_params = validator.params_ordered
hash[:params] = sub_params.map(&:to_json)
end
hash
end

def merge_with(other_param_desc)
Expand Down
16 changes: 12 additions & 4 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def merge_with(other_validator)
raise NotImplementedError, "Dont know how to merge #{self.inspect} with #{other_validator.inspect}"
end

def params_ordered
nil
end

end

# validate arguments type
Expand Down Expand Up @@ -218,8 +222,8 @@ def initialize(param_description, argument, param_group)
prepare_hash_params
end

def hash_params_ordered
@hash_params_ordered ||= _apipie_dsl_data[:params].map do |args|
def params_ordered
@params_ordered ||= _apipie_dsl_data[:params].map do |args|
options = args.find { |arg| arg.is_a? Hash }
options[:parent] = self.param_description
Apipie::ParamDescription.from_dsl_data(param_description.method_description, args)
Expand Down Expand Up @@ -267,15 +271,15 @@ def _default_param_group_scope

def merge_with(other_validator)
if other_validator.is_a? HashValidator
@hash_params_ordered = ParamDescription.unify(self.hash_params_ordered + other_validator.hash_params_ordered)
@params_ordered = ParamDescription.unify(self.params_ordered + other_validator.params_ordered)
prepare_hash_params
else
super
end
end

def prepare_hash_params
@hash_params = hash_params_ordered.reduce({}) do |h, param|
@hash_params = params_ordered.reduce({}) do |h, param|
h.update(param.name.to_sym => param)
end
end
Expand Down Expand Up @@ -373,6 +377,10 @@ def self.build(param_description, argument, options, block)
def description
"Must be an Array of nested elements"
end

def params_ordered
@validator.params_ordered
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/concerns_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

it "replaces placeholders in param names and descriptions" do
create_desc = Apipie["concern_resources#create"].params[:user]
name_param, concern_type_param = create_desc.validator.hash_params_ordered
name_param, concern_type_param = create_desc.validator.params_ordered
name_param.desc.should include "Name of a user"
concern_type_param.name.should == :user_type
end
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def compare_hashes(h1, h2)
param = a.params_ordered.select {|p| p.name == :user }
param.count.should == 1
param.first.validator.class.should eq(Apipie::Validator::HashValidator)
hash_params = param.first.validator.hash_params_ordered
hash_params = param.first.validator.params_ordered
hash_params.count.should == 4
hash_params[0].name == :name
hash_params[1].name == :pass
Expand Down
54 changes: 53 additions & 1 deletion spec/lib/param_description_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@

describe "required params in action aware validator" do

subject { method_description.params[:user].validator.hash_params_ordered }
subject { method_description.params[:user].validator.params_ordered }

let(:required) do
subject.find_all(&:required).map(&:name)
Expand Down Expand Up @@ -240,6 +240,58 @@
end


describe 'sub params' do

context 'with HashValidator' do

subject do
Apipie::ParamDescription.new(method_desc, :param, Hash) do
param :answer, Fixnum
end
end

it "should include the nested params in the json" do
sub_params = subject.to_json[:params]
sub_params.size.should == 1
sub_param = sub_params.first
sub_param[:name].should == "answer"
sub_param[:full_name].should == "param[answer]"
end

end

context 'with NestedValidator' do

subject do
Apipie::ParamDescription.new(method_desc, :param, Array) do
param :answer, Fixnum
end
end

it "should include the nested params in the json" do
sub_params = subject.to_json[:params]
sub_params.size.should == 1
sub_param = sub_params.first
sub_param[:name].should == "answer"
sub_param[:full_name].should == "param[answer]"
end

end

context 'with flat validator' do

subject do
Apipie::ParamDescription.new(method_desc, :param, String)
end

it "should include the nested params in the json" do
subject.to_json[:params].should be_nil
end

end

end

describe "Array with classes" do
it "should be valid for objects included in class array" do
param = Apipie::ParamDescription.new(method_desc, :param, [Fixnum, String])
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/param_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

it "lets reuse the params description in more actions" do
user_create_desc = Apipie["users#create"].params[:user]
user_create_params = user_create_desc.validator.hash_params_ordered.map(&:name)
user_create_params = user_create_desc.validator.params_ordered.map(&:name)

user_update_desc = Apipie["users#update"].params[:user]
user_update_params = user_update_desc.validator.hash_params_ordered.map(&:name)
user_update_params = user_update_desc.validator.params_ordered.map(&:name)

common = user_update_params & user_create_params
common.sort_by(&:to_s).should == user_update_params.sort_by(&:to_s)
end

it "allows using groups is nested param descriptions" do
user_create_desc = Apipie["users#update"].params[:user]
user_create_params = user_create_desc.validator.hash_params_ordered.map(&:name)
user_create_params = user_create_desc.validator.params_ordered.map(&:name)
user_create_params.map(&:to_s).sort.should == %w[membership name pass]
end

it "should allow adding additional params to group" do
user_create_desc = Apipie["users#create"].params[:user]
user_create_params = user_create_desc.validator.hash_params_ordered.map(&:name)
user_create_params = user_create_desc.validator.params_ordered.map(&:name)
user_create_params.map(&:to_s).sort.should == %w[membership name pass permalink]
end

Expand All @@ -34,10 +34,10 @@

it "lets you reuse a group definition from different controller" do
arch_v1_desc = Apipie["1.0#architectures#create"].params[:architecture]
arch_v1_params = arch_v1_desc.validator.hash_params_ordered.map(&:name)
arch_v1_params = arch_v1_desc.validator.params_ordered.map(&:name)

arch_v2_desc = Apipie["2.0#architectures#create"].params[:architecture]
arch_v2_params = arch_v2_desc.validator.hash_params_ordered.map(&:name)
arch_v2_params = arch_v2_desc.validator.params_ordered.map(&:name)

arch_v1_params.sort_by(&:to_s).should == arch_v2_params.sort_by(&:to_s)
end
Expand Down