Skip to content

Commit

Permalink
Give a full_name to parameters in param groups, so
Browse files Browse the repository at this point in the history
documentation can properly access these parameters.
  • Loading branch information
Tim Vandecasteele committed Sep 5, 2012
1 parent 026c26e commit 3715500
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
18 changes: 12 additions & 6 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def params(params)
params
end

def full_name(name)
return "#{@parent.full_name(@element)}[#{name}]" if @parent
name.to_s
end

private
def validates(attrs, validations)
doc_attrs = { :required => validations.keys.include?(:presence) }
Expand All @@ -123,9 +128,10 @@ def validates(attrs, validations)
if desc = validations.delete(:desc)
doc_attrs[:desc] = desc
end

@api.document_attribute(attrs, doc_attrs)


full_attrs = attrs.collect{ |name| { :name => name, :full_name => full_name(name)} }
@api.document_attribute(full_attrs, doc_attrs)

# Validate for presence before any other validators
if validations.has_key?(:presence) && validations[:presence]
validate('presence', validations[:presence], attrs, doc_attrs)
Expand Down Expand Up @@ -168,10 +174,10 @@ def params(&block)
def document_attribute(names, opts)
if @last_description
@last_description[:params] ||= {}

Array(names).each do |name|
@last_description[:params][name.to_s] ||= {}
@last_description[:params][name.to_s].merge!(opts)
@last_description[:params][name[:name].to_s] ||= {}
@last_description[:params][name[:name].to_s].merge!(opts).merge!({:full_name => name[:full_name]})
end
end
end
Expand Down
19 changes: 17 additions & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,7 @@ class CommunicationError < RuntimeError; end
subject.routes.map { |route|
{ :description => route.route_description, :params => route.route_params }
}.should eq [
{ :description => "method", :params => { "ns_param" => { :required => true, :desc => "namespace parameter" }, "method_param" => { :required => false, :desc => "method parameter" } } }
{ :description => "method", :params => { "ns_param" => { :required => true, :desc => "namespace parameter", :full_name=>"ns_param" }, "method_param" => { :required => false, :desc => "method parameter", :full_name=>"method_param" } } }
]
end
it "should merge the parameters of nested namespaces" do
Expand All @@ -1055,7 +1055,22 @@ class CommunicationError < RuntimeError; end
subject.routes.map { |route|
{ :description => route.route_description, :params => route.route_params }
}.should eq [
{ :description => "method", :params => { "ns_param" => { :required => true, :desc => "ns param 2" }, "ns1_param" => { :required => true, :desc => "ns1 param" }, "ns2_param" => { :required => true, :desc => "ns2 param" }, "method_param" => { :required => false, :desc => "method param" } } }
{ :description => "method", :params => { "ns_param" => { :required => true, :desc => "ns param 2", :full_name=>"ns_param" }, "ns1_param" => { :required => true, :desc => "ns1 param", :full_name=>"ns1_param" }, "ns2_param" => { :required => true, :desc => "ns2 param", :full_name=>"ns2_param" }, "method_param" => { :required => false, :desc => "method param", :full_name=>"method_param" } } }
]
end
it "should provide a full_name for parameters in nested groups" do
subject.desc "nesting"
subject.params do
requires :root_param, :desc => "root param"
group :nested do
requires :nested_param, :desc => "nested param"
end
end
subject.get "method" do ; end
subject.routes.map { |route|
{ :description => route.route_description, :params => route.route_params }
}.should eq [
{ :description => "nesting", :params => { "root_param" => { :required => true, :desc => "root param", :full_name=>"root_param" }, "nested_param" => { :required => true, :desc => "nested param", :full_name=>"nested[nested_param]" } } }
]
end
it "should not symbolize params" do
Expand Down

0 comments on commit 3715500

Please sign in to comment.