Skip to content

Commit e81d305

Browse files
committed
Merge pull request #124 from dblock/api-desc-and-params
Added ability to change the description and parameters of the API endpoints generated by grape-swagger.
2 parents 40f7c31 + e1773cd commit e81d305

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#105](https://github.com/tim-vandecasteele/grape-swagger/pull/105): Fixed compatibility with Swagger-UI - [@CraigCottingham](https://github.com/CraigCottingham).
77
* [#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).
88
* [#114](https://github.com/tim-vandecasteele/grape-swagger/pull/114): Added support for generating nested models from composed Grape Entities - [@dspaeth-faber](https://github.com/dspaeth-faber).
9+
* [#124](https://github.com/tim-vandecasteele/grape-swagger/pull/124): Added ability to change the description and parameters of the API endpoints generated by grape-swagger - [@dblock](https://github.com/dblock).
910
* Rewritten .gemspec and removed Jeweler - [@dblock](https://github.com/dblock).
1011
* Added `GrapeEntity::VERSION` - [@dblock](https://github.com/dblock).
1112
* Added Rubocop, Ruby-style linter - [@dblock](https://github.com/dblock).

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ A hash merged into the `info` key of the JSON documentation. This may contain:
129129
* `:license_url`: The URL of the license.
130130
* `:terms_of_service_url`: The URL of the API terms and conditions.
131131
132+
#### api_documentation
133+
134+
Customize the Swagger API documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description".
135+
136+
``` ruby
137+
add_swagger_documentation \
138+
api_documentation: { desc: 'Reticulated splines API swagger-compatible documentation.' }
139+
```
140+
141+
#### specific_api_documentation
142+
143+
Customize the Swagger API specific documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description for specific API".
144+
145+
``` ruby
146+
add_swagger_documentation \
147+
specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }
148+
```
149+
132150
## Swagger Header Parameters
133151

134152
Swagger also supports the documentation of parameters passed in the header. Since grape's ```params[]``` doesn't return header parameters we can specify header parameters seperately in a block after the description.

lib/grape-swagger.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def self.setup(options)
5454
models: [],
5555
info: {},
5656
authorizations: nil,
57-
root_base_path: true
57+
root_base_path: true,
58+
api_documentation: { desc: 'Swagger compatible API description' },
59+
specific_api_documentation: { desc: 'Swagger compatible API description for specific API' }
5860
}
5961

6062
options = defaults.merge(options)
@@ -69,6 +71,8 @@ def self.setup(options)
6971
authorizations = options[:authorizations]
7072
root_base_path = options[:root_base_path]
7173
extra_info = options[:info]
74+
api_doc = options[:api_documentation].dup
75+
specific_api_doc = options[:specific_api_documentation].dup
7276
@@models = options[:models] || []
7377

7478
@@hide_documentation_path = options[:hide_documentation_path]
@@ -79,7 +83,8 @@ def self.setup(options)
7983
end
8084
end
8185

82-
desc 'Swagger compatible API description'
86+
desc api_doc.delete(:desc), params: api_doc.delete(:params)
87+
@last_description.merge!(api_doc)
8388
get @@mount_path do
8489
header['Access-Control-Allow-Origin'] = '*'
8590
header['Access-Control-Request-Method'] = '*'
@@ -118,13 +123,14 @@ def self.setup(options)
118123
output
119124
end
120125

121-
desc 'Swagger compatible API description for specific API', params: {
126+
desc specific_api_doc.delete(:desc), params: {
122127
'name' => {
123128
desc: 'Resource name of mounted API',
124129
type: 'string',
125130
required: true
126131
}
127-
}
132+
}.merge(specific_api_doc.delete(:params) || {})
133+
@last_description.merge!(specific_api_doc)
128134
get "#{@@mount_path}/:name" do
129135
header['Access-Control-Allow-Origin'] = '*'
130136
header['Access-Control-Request-Method'] = '*'

spec/api_description_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'spec_helper'
2+
3+
describe 'API Description' do
4+
context 'with no additional options' do
5+
subject do
6+
Class.new(Grape::API) do
7+
add_swagger_documentation
8+
end
9+
end
10+
11+
it 'describes the API with defaults' do
12+
routes = subject.endpoints.first.routes
13+
expect(routes.count).to eq 2
14+
expect(routes.first.route_description).to eq 'Swagger compatible API description'
15+
expect(routes.first.route_params).to eq({})
16+
expect(routes.last.route_description).to eq 'Swagger compatible API description for specific API'
17+
expect(routes.last.route_params).to eq('name' => { desc: 'Resource name of mounted API', type: 'string', required: true })
18+
end
19+
end
20+
21+
context 'with additional options' do
22+
subject do
23+
Class.new(Grape::API) do
24+
add_swagger_documentation \
25+
api_documentation: { desc: 'First', params: { x: 1 }, xx: 11 },
26+
specific_api_documentation: { desc: 'Second', params: { y: 42 }, yy: 4242 }
27+
end
28+
end
29+
30+
it 'describes the API with defaults' do
31+
routes = subject.endpoints.first.routes
32+
expect(routes.count).to eq 2
33+
expect(routes.first.route_description).to eq 'First'
34+
expect(routes.first.route_params).to eq(x: 1)
35+
expect(routes.first.route_xx).to eq(11)
36+
expect(routes.last.route_description).to eq 'Second'
37+
expect(routes.last.route_params).to eq('name' => { desc: 'Resource name of mounted API', type: 'string', required: true }, y: 42)
38+
expect(routes.last.route_yy).to eq(4242)
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)