Skip to content

Commit 2f46a71

Browse files
committed
Fixed desc to take only hashes.
1 parent 17ec92c commit 2f46a71

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ end
250250

251251
## Describing and Inspecting an API
252252

253-
Grape lets you add a description to an API along with any other optional elements that can also be inspected at runtime.
253+
Grape lets you add a description to an API along with any other optional elements that can also be inspected at runtime.
254254
This can be useful for generating documentation.
255255

256256
```ruby
@@ -263,9 +263,9 @@ class TwitterAPI < Grape::API
263263
api.version
264264
end
265265

266-
desc "Reverses a string.", { :params => [
266+
desc "Reverses a string.", { :params =>
267267
{ "s" => { :desc => "string to reverse", :type => "string" }}
268-
]}
268+
}
269269
get "reverse" do
270270
params[:s].reverse
271271
end

lib/grape/endpoint.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ def prepare_routes
5050
prepared_path = prepare_path(path)
5151
path = compile_path(path, !options[:app])
5252
regex = Rack::Mount::RegexpWithNamedGroups.new(path)
53-
path_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
54-
path_params |= (options[:route_options][:params] || [])
53+
path_params = {}
54+
# named parameters in the api path
55+
named_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
56+
named_params.each { |named_param| path_params[named_param] = "" }
57+
# route parameters declared via desc or appended to the api declaration
58+
route_params = (options[:route_options][:params] || {})
59+
path_params.merge!(route_params)
5560
request_method = (method.to_s.upcase unless method == :any)
5661
routes << Route.new(options[:route_options].clone.merge({
5762
:prefix => settings[:root_prefix],

spec/grape/api_spec.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ class TwitterAPI < Grape::API
747747
end
748748
describe "api structure with additional parameters" do
749749
before(:each) do
750-
subject.get 'split/:string', { :params => [ "token" ], :optional_params => [ "limit" ] } do
750+
subject.get 'split/:string', { :params => { "token" => "a token" }, :optional_params => { "limit" => "the limit" } } do
751751
params[:string].split(params[:token], (params[:limit] || 0).to_i)
752752
end
753753
end
@@ -761,8 +761,8 @@ class TwitterAPI < Grape::API
761761
end
762762
it "should set route_params" do
763763
subject.routes.size.should == 1
764-
subject.routes[0].route_params.should == [ "string", "token" ]
765-
subject.routes[0].route_optional_params.should == [ "limit" ]
764+
subject.routes[0].route_params.should == { "string" => "", "token" => "a token" }
765+
subject.routes[0].route_optional_params.should == { "limit" => "the limit" }
766766
end
767767
end
768768
end
@@ -785,6 +785,17 @@ class TwitterAPI < Grape::API
785785
subject.routes[0].route_description.should == "ping method"
786786
end
787787
end
788+
describe "single method with a an array of params and a desc hash block" do
789+
before(:each) do
790+
subject.desc "ping method", { :params => { "x" => "y" } }
791+
subject.get "ping/:x" do
792+
'pong'
793+
end
794+
end
795+
it "returns route description" do
796+
subject.routes[0].route_description.should == "ping method"
797+
end
798+
end
788799
describe "api structure with multiple methods and descriptions" do
789800
before(:each) do
790801
class JitterAPI < Grape::API
@@ -813,9 +824,9 @@ class LitterAPI < Grape::API
813824
end
814825
desc "third method", :details => "details of third method"
815826
get "third" do; end
816-
desc "Reverses a string.", { :params => [
827+
desc "Reverses a string.", { :params =>
817828
{ "s" => { :desc => "string to reverse", :type => "string" }}
818-
]}
829+
}
819830
get "reverse" do
820831
params[:s].reverse
821832
end
@@ -828,7 +839,7 @@ class LitterAPI < Grape::API
828839
LitterAPI::routes[2].route_foo.should == "bar"
829840
LitterAPI::routes[3].route_description.should == "third method"
830841
LitterAPI::routes[4].route_description.should == "Reverses a string."
831-
LitterAPI::routes[4].route_params.should == [{ "s" => { :desc => "string to reverse", :type => "string" }}]
842+
LitterAPI::routes[4].route_params.should == { "s" => { :desc => "string to reverse", :type => "string" }}
832843
end
833844
end
834845
end

0 commit comments

Comments
 (0)