Skip to content

Commit df82041

Browse files
renierdblock
authored andcommitted
Added support for namespace descriptions.
1 parent 982f71a commit df82041

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Next Release
22

3+
* [#94](https://github.com/tim-vandecasteele/grape-swagger/pull/94): Added support for namespace descriptions - [@renier](https://github.com/renier).
34
* [#110](https://github.com/tim-vandecasteele/grape-swagger/pull/110), [#111](https://github.com/tim-vandecasteele/grape-swagger/pull/111) - Added `responseModel` support - [@bagilevi](https://github.com/bagilevi).
45
* [#105](https://github.com/tim-vandecasteele/grape-swagger/pull/105): Fixed compatibility with Swagger-UI - [@CraigCottingham](https://github.com/CraigCottingham).
56
* [#87](https://github.com/tim-vandecasteele/grape-swagger/pull/87): Fixed mapping of `default` to `defaultValue` - [@m-o-e](https://github.com/m-o-e).

lib/grape-swagger.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module Grape
55
class API
66
class << self
7-
attr_reader :combined_routes
7+
attr_reader :combined_routes, :combined_namespaces
88

99
def add_swagger_documentation(options = {})
1010
documentation_class = create_documentation_class
@@ -13,7 +13,6 @@ def add_swagger_documentation(options = {})
1313
mount(documentation_class)
1414

1515
@combined_routes = {}
16-
1716
routes.each do |route|
1817
route_match = route.route_path.split(route.route_prefix).last.match('\/([\w|-]*?)[\.\/\(]')
1918
next if route_match.nil?
@@ -24,6 +23,12 @@ def add_swagger_documentation(options = {})
2423
next if @@hide_documentation_path && route.route_path.include?(@@mount_path)
2524
@combined_routes[resource] << route
2625
end
26+
27+
@combined_namespaces = {}
28+
endpoints.each do |endpoint|
29+
ns = endpoint.settings.stack.last[:namespace]
30+
@combined_namespaces[ns.space] = ns if ns
31+
end
2732
end
2833

2934
private
@@ -80,6 +85,7 @@ def self.setup(options)
8085
header['Access-Control-Request-Method'] = '*'
8186

8287
routes = target_class.combined_routes
88+
namespaces = target_class.combined_namespaces
8389

8490
if @@hide_documentation_path
8591
routes.reject! { |route, _value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
@@ -89,9 +95,13 @@ def self.setup(options)
8995
next if routes[local_route].all?(&:route_hidden)
9096

9197
url_format = '.{format}' unless @@hide_format
98+
99+
description = namespaces[local_route] && namespaces[local_route].options[:desc]
100+
description ||= "Operations about #{local_route.pluralize}"
101+
92102
{
93103
path: "/#{local_route}#{url_format}",
94-
description: "Operations about #{local_route.pluralize}"
104+
description: description
95105
}
96106
end.compact
97107

spec/grape-swagger_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
Grape::API.should respond_to :add_swagger_documentation
1111
end
1212

13+
it 'added combined-namespaces' do
14+
Grape::API.should respond_to :combined_namespaces
15+
end
1316
end

spec/non_default_api_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,31 @@ def app
575575
end
576576

577577
end
578+
579+
context 'documented namespace description' do
580+
before :all do
581+
class NamespaceWithDescAPI < Grape::API
582+
namespace :aspace, desc: 'Description for aspace' do
583+
desc 'This gets something.'
584+
get '/something' do
585+
{ bla: 'something' }
586+
end
587+
end
588+
add_swagger_documentation format: :json
589+
end
590+
get '/swagger_doc'
591+
end
592+
593+
def app
594+
NamespaceWithDescAPI
595+
end
596+
597+
subject do
598+
JSON.parse(last_response.body)['apis'][0]
599+
end
600+
601+
it 'shows the namespace description in the json spec' do
602+
expect(subject['description']).to eql('Description for aspace')
603+
end
604+
end
578605
end

0 commit comments

Comments
 (0)