Skip to content

Commit 62a6cfa

Browse files
author
Renier Morales
committed
Merge branch 'namespace_description'
This is on top of pull request ruby-grape#105. Conflicts: lib/grape-swagger.rb
2 parents 50e968c + 19eed77 commit 62a6cfa

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/grape-swagger.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Grape
44
class API
55
class << self
6-
attr_reader :combined_routes
6+
attr_reader :combined_routes, :combined_namespaces
77

88
def add_swagger_documentation(options={})
99
documentation_class = create_documentation_class
@@ -26,6 +26,14 @@ def add_swagger_documentation(options={})
2626
@combined_routes[resource] << route
2727
end
2828
end
29+
30+
@combined_namespaces = {}
31+
32+
endpoints.each do |endpoint|
33+
ns = endpoint.settings.stack.last[:namespace]
34+
@combined_namespaces[ns.space] = ns if ns
35+
end
36+
2937
end
3038

3139
private
@@ -85,6 +93,7 @@ def self.setup(options)
8593
header['Access-Control-Request-Method'] = '*'
8694

8795
routes = target_class::combined_routes
96+
spaces = target_class::combined_namespaces
8897

8998
if @@hide_documentation_path
9099
routes.reject!{ |route, value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
@@ -95,10 +104,11 @@ def self.setup(options)
95104

96105
url_base = parse_path(route.route_path.gsub('(.:format)', ''), route.route_version) if include_base_url
97106
url_format = '.{format}' unless @@hide_format
98-
{
99-
:path => "/#{local_route}#{url_format}",
100-
:description => "Operations about #{local_route.pluralize}"
101-
}
107+
108+
api = { :path => "#{url_base}/#{local_route}#{url_format}" }
109+
description = spaces[local_route] && spaces[local_route].options[:desc]
110+
api[:description] = description if description
111+
api
102112
end.compact
103113

104114
output = {

spec/grape-swagger_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
Grape::API.should respond_to :combined_routes
77
end
88

9+
it "added combined-namespaces" do
10+
Grape::API.should respond_to :combined_namespaces
11+
end
12+
913
it "added add_swagger_documentation" do
1014
Grape::API.should respond_to :add_swagger_documentation
1115
end

spec/non_default_api_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,29 @@ def app; SimpleJSONFormattedAPI; end
546546
end
547547

548548
end
549+
550+
context "documented namespace description" do
551+
before :all do
552+
class NamespaceWithDescAPI < Grape::API
553+
namespace :aspace, desc: "Description for aspace" do
554+
desc 'This gets something.'
555+
get '/something' do
556+
{ bla: 'something' }
557+
end
558+
end
559+
add_swagger_documentation format: :json
560+
end
561+
get '/swagger_doc'
562+
end
563+
564+
def app; NamespaceWithDescAPI; end
565+
566+
subject do
567+
JSON.parse(last_response.body)['apis'][0]
568+
end
569+
570+
it "shows the namespace description in the json spec" do
571+
expect(subject['description']).to eql('Description for aspace')
572+
end
573+
end
549574
end

0 commit comments

Comments
 (0)