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

Add possibility for namespace parameters. #204

Merged
merged 3 commits into from
Jul 19, 2012
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
7 changes: 6 additions & 1 deletion lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'rack/auth/basic'
require 'rack/auth/digest/md5'
require 'logger'
require 'grape/util/deep_merge'

module Grape
# The API class is the primary entry point for
Expand Down Expand Up @@ -287,7 +288,7 @@ def route(methods, paths = ['/'], route_options = {}, &block)
endpoint_options = {
:method => methods,
:path => paths,
:route_options => (route_options || {}).merge(@last_description || {})
:route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
}
endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block)

Expand All @@ -313,9 +314,13 @@ def patch(paths = ['/'], options = {}, &block); route('PATCH', paths, options, &

def namespace(space = nil, &block)
if space || block_given?
previous_namespace_description = @namespace_description
@namespace_description = (@namespace_description || {}).deep_merge(@last_description || {})
@last_description = nil
nest(block) do
set(:namespace, space.to_s) if space
end
@namespace_description = previous_namespace_description
else
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
end
Expand Down
23 changes: 23 additions & 0 deletions lib/grape/util/deep_merge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Hash
# deep_merge from rails
# activesupport/lib/active_support/core_ext/hash/deep_merge.rb
# Returns a new hash with +self+ and +other_hash+ merged recursively.
#
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
#
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
def deep_merge(other_hash)
dup.deep_merge!(other_hash)
end

# Same as +deep_merge+, but modifies +self+.
def deep_merge!(other_hash)
other_hash.each_pair do |k,v|
tv = self[k]
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
end
self
end
end
4 changes: 2 additions & 2 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ def document_attribute(names, opts)
@last_description[:params] ||= {}

Array(names).each do |name|
@last_description[:params][name.to_sym] ||= {}
@last_description[:params][name.to_sym].merge!(opts)
@last_description[:params][name.to_s] ||= {}
@last_description[:params][name.to_s].merge!(opts)
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,50 @@ def three
{ :description => "Reverses a string.", :params => { "s" => { :desc => "string to reverse", :type => "string" } } }
]
end
it "should merge the parameters of the namespace with the parameters of the method" do
subject.desc "namespace"
subject.params do
requires :ns_param, :desc => "namespace parameter"
end
subject.namespace "ns" do
desc "method"
params do
optional :method_param, :desc => "method parameter"
end
get "method" do ; end
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" } } }
]
end
it "should merge the parameters of nested namespaces" do
subject.desc "ns1"
subject.params do
optional :ns_param, :desc => "ns param 1"
requires :ns1_param, :desc => "ns1 param"
end
subject.namespace "ns1" do
desc "ns2"
params do
requires :ns_param, :desc => "ns param 2"
requires :ns2_param, :desc => "ns2 param"
end
namespace "ns2" do
desc "method"
params do
optional :method_param, :desc => "method param"
end
get "method" do ; end
end
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" } } }
]
end
it "should not symbolize params" do
subject.desc "Reverses a string.", { :params =>
{ "s" => { :desc => "string to reverse", :type => "string" }}
Expand Down