grape-swagger 0.11.0 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -1
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +14 -22
- data/.travis.yml +7 -4
- data/CHANGELOG.md +53 -26
- data/Gemfile +1 -1
- data/README.md +414 -327
- data/RELEASING.md +3 -4
- data/example/api/endpoints.rb +132 -0
- data/example/api/entities.rb +18 -0
- data/example/config.ru +36 -2
- data/example/example_requests.postman_collection +146 -0
- data/example/swagger-example.png +0 -0
- data/grape-swagger.gemspec +9 -6
- data/lib/grape-swagger.rb +69 -99
- data/lib/grape-swagger/doc_methods.rb +69 -544
- data/lib/grape-swagger/doc_methods/data_type.rb +77 -0
- data/lib/grape-swagger/doc_methods/extensions.rb +75 -0
- data/lib/grape-swagger/doc_methods/move_params.rb +153 -0
- data/lib/grape-swagger/doc_methods/operation_id.rb +27 -0
- data/lib/grape-swagger/doc_methods/optional_object.rb +15 -0
- data/lib/grape-swagger/doc_methods/parse_params.rb +113 -0
- data/lib/grape-swagger/doc_methods/path_string.rb +29 -0
- data/lib/grape-swagger/doc_methods/produces_consumes.rb +12 -0
- data/lib/grape-swagger/doc_methods/status_codes.rb +17 -0
- data/lib/grape-swagger/doc_methods/tag_name_description.rb +26 -0
- data/lib/grape-swagger/endpoint.rb +317 -0
- data/lib/grape-swagger/version.rb +1 -1
- data/spec/lib/data_type_spec.rb +57 -0
- data/spec/lib/endpoint_spec.rb +6 -0
- data/spec/lib/extensions_spec.rb +127 -0
- data/spec/lib/move_params_spec.rb +298 -0
- data/spec/lib/operation_id_spec.rb +24 -0
- data/spec/lib/optional_object_spec.rb +40 -0
- data/spec/lib/path_string_spec.rb +38 -0
- data/spec/lib/produces_consumes_spec.rb +98 -0
- data/spec/markdown/kramdown_adapter_spec.rb +2 -9
- data/spec/markdown/redcarpet_adapter_spec.rb +2 -16
- data/spec/spec_helper.rb +7 -13
- data/spec/support/api_swagger_v2_result.rb +204 -0
- data/spec/support/namespace_tags.rb +73 -0
- data/spec/support/the_api_entities.rb +52 -0
- data/spec/support/the_paths_definitions.rb +94 -0
- data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +32 -0
- data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +151 -0
- data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +109 -0
- data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +124 -0
- data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +51 -0
- data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +44 -0
- data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +56 -0
- data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +146 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +197 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +151 -0
- data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +217 -0
- data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +64 -0
- data/spec/swagger_v2/api_swagger_v2_response_spec.rb +184 -0
- data/spec/swagger_v2/api_swagger_v2_spec.rb +207 -0
- data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +121 -0
- data/spec/{boolean_params_spec.rb → swagger_v2/boolean_params_spec.rb} +2 -2
- data/spec/{default_api_spec.rb → swagger_v2/default_api_spec.rb} +40 -36
- data/spec/swagger_v2/description_not_initialized.rb +39 -0
- data/spec/{float_api_spec.rb → swagger_v2/float_api_spec.rb} +2 -2
- data/spec/{form_params_spec.rb → swagger_v2/form_params_spec.rb} +9 -9
- data/spec/{grape-swagger_spec.rb → swagger_v2/grape-swagger_spec.rb} +0 -0
- data/spec/swagger_v2/hide_api_spec.rb +131 -0
- data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
- data/spec/swagger_v2/namespace_tags_prefix_spec.rb +84 -0
- data/spec/swagger_v2/namespace_tags_spec.rb +76 -0
- data/spec/{namespaced_api_spec.rb → swagger_v2/namespaced_api_spec.rb} +6 -26
- data/spec/{param_type_spec.rb → swagger_v2/param_type_spec.rb} +10 -8
- data/spec/{param_values_spec.rb → swagger_v2/param_values_spec.rb} +52 -22
- data/spec/swagger_v2/params_array_spec.rb +63 -0
- data/spec/swagger_v2/params_hash_spec.rb +65 -0
- data/spec/swagger_v2/params_nested_spec.rb +63 -0
- data/spec/{reference_entity.rb → swagger_v2/reference_entity.rb} +18 -23
- data/spec/swagger_v2/response_model_spec.rb +212 -0
- data/spec/swagger_v2/simple_mounted_api_spec.rb +264 -0
- metadata +175 -90
- data/example/api.rb +0 -66
- data/lib/grape-swagger/markdown.rb +0 -23
- data/spec/api_description_spec.rb +0 -43
- data/spec/api_global_models_spec.rb +0 -77
- data/spec/api_models_spec.rb +0 -364
- data/spec/api_paths_spec.rb +0 -128
- data/spec/api_root_spec.rb +0 -30
- data/spec/api_with_nil_types.rb +0 -50
- data/spec/api_with_path_versioning_spec.rb +0 -33
- data/spec/api_with_prefix_and_namespace_spec.rb +0 -32
- data/spec/api_with_standalone_namespace_spec.rb +0 -215
- data/spec/array_entity_spec.rb +0 -34
- data/spec/array_params_spec.rb +0 -85
- data/spec/grape-swagger_helper_spec.rb +0 -152
- data/spec/group_params_spec.rb +0 -31
- data/spec/hash_params_spec.rb +0 -30
- data/spec/hide_api_spec.rb +0 -124
- data/spec/i18n_spec.rb +0 -364
- data/spec/markdown/markdown_spec.rb +0 -27
- data/spec/mounted_target_class_spec.rb +0 -63
- data/spec/mutually_exclusive_spec.rb +0 -36
- data/spec/non_default_api_spec.rb +0 -733
- data/spec/response_model_spec.rb +0 -121
- data/spec/simple_mounted_api_spec.rb +0 -213
- data/spec/support/i18n_helper.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 881259fe2427869fbd1dbe2af3925eff5c6ec991
|
4
|
+
data.tar.gz: 6557a2cee402f5f5fd8f4d03ca07b4abcb423531
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ecc8ed62a66df220e26243a208270fedbf80b91af6b5b26a8d5bb4f99486cdfd14cdd721929c0340dabffa739d49142b27102d89ef9081c0ff1ac5098bc616a6
|
7
|
+
data.tar.gz: 4bbf258316ad6e4ee1a56b5016e86150f29a63eb71047a943975979c60330b86e5f17e9e3554afc76f676d5a72b0af363efc09e25324113bb0d7b06c9cb656fc
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
@@ -1,42 +1,42 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on
|
3
|
+
# on 2015-08-19 12:17:53 -0400 using RuboCop version 0.33.0.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
7
7
|
# versions of RuboCop, may require this file to be generated again.
|
8
8
|
|
9
|
-
# Offense count:
|
9
|
+
# Offense count: 10
|
10
10
|
Metrics/AbcSize:
|
11
|
-
Max:
|
11
|
+
Max: 176
|
12
12
|
|
13
13
|
# Offense count: 1
|
14
14
|
# Configuration parameters: CountComments.
|
15
15
|
Metrics/ClassLength:
|
16
|
-
Max:
|
16
|
+
Max: 250
|
17
17
|
|
18
|
-
# Offense count:
|
18
|
+
# Offense count: 6
|
19
19
|
Metrics/CyclomaticComplexity:
|
20
|
-
Max:
|
20
|
+
Max: 38
|
21
21
|
|
22
|
-
# Offense count:
|
22
|
+
# Offense count: 304
|
23
23
|
# Configuration parameters: AllowURI, URISchemes.
|
24
24
|
Metrics/LineLength:
|
25
|
-
Max:
|
25
|
+
Max: 242
|
26
26
|
|
27
|
-
# Offense count:
|
27
|
+
# Offense count: 21
|
28
28
|
# Configuration parameters: CountComments.
|
29
29
|
Metrics/MethodLength:
|
30
|
-
Max:
|
30
|
+
Max: 139
|
31
31
|
|
32
32
|
# Offense count: 1
|
33
33
|
# Configuration parameters: CountComments.
|
34
34
|
Metrics/ModuleLength:
|
35
|
-
Max:
|
35
|
+
Max: 200
|
36
36
|
|
37
|
-
# Offense count:
|
37
|
+
# Offense count: 4
|
38
38
|
Metrics/PerceivedComplexity:
|
39
|
-
Max:
|
39
|
+
Max: 38
|
40
40
|
|
41
41
|
# Offense count: 8
|
42
42
|
Style/ClassVars:
|
@@ -44,14 +44,13 @@ Style/ClassVars:
|
|
44
44
|
- 'example/api.rb'
|
45
45
|
- 'lib/grape-swagger/doc_methods.rb'
|
46
46
|
|
47
|
-
# Offense count:
|
47
|
+
# Offense count: 90
|
48
48
|
Style/Documentation:
|
49
49
|
Enabled: false
|
50
50
|
|
51
51
|
# Offense count: 2
|
52
52
|
Style/DoubleNegation:
|
53
53
|
Exclude:
|
54
|
-
- 'lib/grape-swagger/doc_methods.rb'
|
55
54
|
|
56
55
|
# Offense count: 3
|
57
56
|
# Configuration parameters: Exclude.
|
@@ -61,16 +60,10 @@ Style/FileName:
|
|
61
60
|
- 'spec/grape-swagger_helper_spec.rb'
|
62
61
|
- 'spec/grape-swagger_spec.rb'
|
63
62
|
|
64
|
-
# Offense count: 1
|
65
|
-
Style/MultilineBlockChain:
|
66
|
-
Exclude:
|
67
|
-
- 'lib/grape-swagger/doc_methods.rb'
|
68
|
-
|
69
63
|
# Offense count: 1
|
70
64
|
# Configuration parameters: NamePrefix, NamePrefixBlacklist.
|
71
65
|
Style/PredicateName:
|
72
66
|
Exclude:
|
73
|
-
- 'lib/grape-swagger/doc_methods.rb'
|
74
67
|
|
75
68
|
# Offense count: 4
|
76
69
|
# Cop supports --auto-correct.
|
@@ -78,4 +71,3 @@ Style/PredicateName:
|
|
78
71
|
Style/RegexpLiteral:
|
79
72
|
Exclude:
|
80
73
|
- 'lib/grape-swagger.rb'
|
81
|
-
- 'lib/grape-swagger/doc_methods.rb'
|
data/.travis.yml
CHANGED
@@ -3,16 +3,19 @@ language: ruby
|
|
3
3
|
sudo: false
|
4
4
|
|
5
5
|
rvm:
|
6
|
+
- 2.3.0
|
6
7
|
- 2.2.3
|
7
8
|
- 2.1.7
|
8
9
|
- jruby-19mode
|
9
10
|
- jruby-9.0.5.0
|
10
11
|
- rbx-2
|
11
12
|
|
12
|
-
env:
|
13
|
-
- GRAPE_VERSION=0.16.2
|
14
|
-
- GRAPE_VERSION=HEAD
|
15
|
-
|
16
13
|
matrix:
|
17
14
|
allow_failures:
|
18
15
|
- rvm: rbx-2
|
16
|
+
- rvm: jruby-19mode
|
17
|
+
env:
|
18
|
+
- GRAPE_VERSION=0.12.0
|
19
|
+
- GRAPE_VERSION=0.13.0
|
20
|
+
- GRAPE_VERSION=0.14.0
|
21
|
+
# - GRAPE_VERSION=HEAD
|
data/CHANGELOG.md
CHANGED
@@ -1,28 +1,55 @@
|
|
1
|
-
### 0.
|
1
|
+
### 0.20.0 / 2016-04-09
|
2
2
|
|
3
3
|
#### Features
|
4
4
|
|
5
|
-
|
5
|
+
[#371](https://github.com/ruby-grape/grape-swagger/pull/371)
|
6
6
|
|
7
|
-
|
7
|
+
- adds param type `body` handling
|
8
8
|
|
9
|
-
|
10
|
-
* [#347](https://github.com/ruby-grape/grape-swagger/pull/347): Fixed typeref when both :using and :as are provided in exposure but no :type in documentation - [@c910335](https://github.com/c910335).
|
9
|
+
[#367](https://github.com/ruby-grape/grape-swagger/pull/367)
|
11
10
|
|
12
|
-
|
11
|
+
- set default `type: Integer` and `required: true` for path params,
|
12
|
+
if they wasn't specified inside the `params` bloack as required
|
13
|
+
|
14
|
+
[#365](https://github.com/ruby-grape/grape-swagger/pull/365)
|
15
|
+
|
16
|
+
- fixes passing markdown with redcarpet even with nil description and detail
|
17
|
+
|
18
|
+
[#358](https://github.com/ruby-grape/grape-swagger/pull/358)
|
19
|
+
|
20
|
+
- removes `allowMultiple` property from params
|
21
|
+
- adds `format` to definition property
|
22
|
+
- renames `defaultValue` to `default`
|
23
|
+
|
24
|
+
|
25
|
+
[#356](https://github.com/ruby-grape/grape-swagger/pull/356)
|
26
|
+
|
27
|
+
- adds `consumes` setting
|
28
|
+
- refactoring
|
29
|
+
|
30
|
+
[#354](https://github.com/ruby-grape/grape-swagger/pull/354) some improvements
|
31
|
+
|
32
|
+
- fixes setting of `base_path` and `host`;
|
33
|
+
- adds possibility to configure the setting of `version` and `base_path` in documented path;
|
34
|
+
- adds `operationId`
|
35
|
+
|
36
|
+
[#353](https://github.com/ruby-grape/grape-swagger/pull/353) resolves issue #352
|
37
|
+
|
38
|
+
### 0.10.4 (Next)
|
13
39
|
|
14
|
-
|
15
|
-
* [#321](https://github.com/ruby-grape/grape-swagger/pull/321): Fixed handling paths containing uppercase letters - [@gekola](https://github.com/gekola).
|
40
|
+
[#344](https://github.com/ruby-grape/grape-swagger/pull/) Namespace based tag include in Swagger Json
|
16
41
|
|
17
|
-
|
42
|
+
* Namespace based tagging help the swagger GUI to group the API list
|
18
43
|
|
19
|
-
|
44
|
+
### 0.10.3 (Next)
|
20
45
|
|
21
|
-
|
46
|
+
[#336](https://github.com/ruby-grape/grape-swagger/pull/336) changes of swagger-2.0 fork, to support it
|
22
47
|
|
23
|
-
*
|
24
|
-
*
|
25
|
-
*
|
48
|
+
* updates gems, corrects parameter, which is in array, make rubocop happy
|
49
|
+
* runs under 2.3
|
50
|
+
* documents produces of an end-point
|
51
|
+
* Update api_swagger_v2_format-content_type_spec.rb
|
52
|
+
* upgrades to grape 0.14.x; grape-entity 0.5.x
|
26
53
|
|
27
54
|
### 0.10.2 (August 19, 2015)
|
28
55
|
|
@@ -136,13 +163,13 @@
|
|
136
163
|
|
137
164
|
* Added Rails 4 support - [@jrhe](https://github.com/jrhe).
|
138
165
|
* Fix: document APIs at root level - [@dblock](https://github.com/dblock).
|
139
|
-
* Added support for procs in basepath - [@
|
166
|
+
* Added support for procs in basepath - [@ruby-grape](https://github.com/ruby-grape).
|
140
167
|
* Support both `:desc` and `:description` when describing parameters - [@dblock](https://github.com/dblock).
|
141
168
|
* Fix: allow parameters such as `name[]` - [@dblock](https://github.com/dblock).
|
142
169
|
|
143
170
|
### 0.5.0 (March 28, 2013)
|
144
171
|
|
145
|
-
* Added Grape 0.5.0 support - [@
|
172
|
+
* Added Grape 0.5.0 support - [@ruby-grape](https://github.com/ruby-grape).
|
146
173
|
|
147
174
|
### 0.4.0 (March 28, 2013)
|
148
175
|
|
@@ -151,28 +178,28 @@
|
|
151
178
|
### 0.3.0 (October 19, 2012)
|
152
179
|
|
153
180
|
* Added version support - [@agileanimal](https://github.com/agileanimal), [@fknappe](https://github.com/fknappe).
|
154
|
-
* Added support for nested parameters - [@
|
181
|
+
* Added support for nested parameters - [@ruby-grape](https://github.com/ruby-grape).
|
155
182
|
* Added basic support for specifying parameters that need to be passed in the header - [@agileanimal](https://github.com/agileanimal).
|
156
|
-
* Add possibility to hide the documentation paths in the generated swagger documentation - [@
|
183
|
+
* Add possibility to hide the documentation paths in the generated swagger documentation - [@ruby-grape](https://github.com/ruby-grape).
|
157
184
|
|
158
185
|
### 0.2.1 (August 17, 2012)
|
159
186
|
|
160
|
-
* Added support for markdown in notes field - [@
|
187
|
+
* Added support for markdown in notes field - [@ruby-grape](https://github.com/ruby-grape).
|
161
188
|
* Fix: compatibility with Rails - [@qwert666](https://github.com/qwert666).
|
162
|
-
* Fix: swagger UI history - [@
|
189
|
+
* Fix: swagger UI history - [@ruby-grape](https://github.com/ruby-grape).
|
163
190
|
|
164
191
|
### 0.2.0 (July 27, 2012)
|
165
192
|
|
166
|
-
* Use resource as root for swagger - [@
|
167
|
-
* Added support for file uploads, and proper `paramType` - [@
|
193
|
+
* Use resource as root for swagger - [@ruby-grape](https://github.com/ruby-grape).
|
194
|
+
* Added support for file uploads, and proper `paramType` - [@ruby-grape](https://github.com/ruby-grape).
|
168
195
|
* Added tests - [@nathanvda](https://github.com/nathanvda).
|
169
196
|
|
170
197
|
### 0.1.0 (July 19, 2012)
|
171
198
|
|
172
|
-
* Added some configurability to the generated documentation - [@
|
173
|
-
* Adapted to rails plugin structure - [@
|
174
|
-
* Allowed cross origin, so swagger can be used from official site - [@
|
199
|
+
* Added some configurability to the generated documentation - [@ruby-grape](https://github.com/ruby-grape).
|
200
|
+
* Adapted to rails plugin structure - [@ruby-grape](https://github.com/ruby-grape).
|
201
|
+
* Allowed cross origin, so swagger can be used from official site - [@ruby-grape](https://github.com/ruby-grape).
|
175
202
|
|
176
203
|
### 0.0.0 (July 19, 2012)
|
177
204
|
|
178
|
-
* Initial public release - [@
|
205
|
+
* Initial public release - [@ruby-grape](https://github.com/ruby-grape).
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,52 +1,76 @@
|
|
1
|
-
|
1
|
+
This is work in progress for bringing grape-swagger to [swagger-spec (OpenAPI) 2.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md) spec. Re-added/reimplemented features from [grape-swagger 1.2](https://github.com/ruby-grape/grape-swagger/tree/swagger-1.2) could be found in the ToC.
|
2
2
|
|
3
|
-
|
3
|
+
##### Table of Contents
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
[What is grape-swagger?](#what)
|
6
|
+
[Related Projects](#related)
|
7
|
+
[Swagger-Spec](#swagger-spec)
|
8
|
+
[Installation](#install)
|
9
|
+
[Usage](#usage)
|
10
|
+
[Configure](#configure)
|
11
|
+
[Routes Configuration](#routes)
|
12
|
+
[Markdown](#md_usage)
|
13
|
+
[Response documentation](#response)
|
14
|
+
[Extensions](#extensions)
|
15
|
+
[Example](#example)
|
7
16
|
|
8
|
-
|
17
|
+
For how to use at the moment see [v2 specs](tree/master/spec/swagger_v2) and or [Hussars](https://github.com/LeFnord/hussars) sample app.
|
9
18
|
|
10
19
|
[![Gem Version](https://badge.fury.io/rb/grape-swagger.svg)](http://badge.fury.io/rb/grape-swagger)
|
11
|
-
[![Build Status](https://travis-ci.org/ruby-grape/grape-swagger.svg?branch=swagger-
|
20
|
+
[![Build Status](https://travis-ci.org/ruby-grape/grape-swagger.svg?branch=swagger-2.0)](https://travis-ci.org/ruby-grape/grape-swagger)
|
12
21
|
[![Dependency Status](https://gemnasium.com/ruby-grape/grape-swagger.svg)](https://gemnasium.com/ruby-grape/grape-swagger)
|
13
22
|
[![Code Climate](https://codeclimate.com/github/ruby-grape/grape-swagger.svg)](https://codeclimate.com/github/ruby-grape/grape-swagger)
|
14
23
|
|
24
|
+
<a name="what" />
|
15
25
|
## What is grape-swagger?
|
16
26
|
|
17
27
|
The grape-swagger gem provides an autogenerated documentation for your [Grape](https://github.com/ruby-grape/grape) API. The generated documentation is Swagger-compliant, meaning it can easily be discovered in [Swagger UI](https://github.com/wordnik/swagger-ui). You should be able to point [the petstore demo](http://petstore.swagger.io/) to your API.
|
18
28
|
|
19
|
-
![Demo Screenshot](example/
|
29
|
+
![Demo Screenshot](example/swagger-example.png)
|
20
30
|
|
31
|
+
These screenshot is based on the [Hussars](https://github.com/LeFnord/hussars) sample app.
|
32
|
+
|
33
|
+
|
34
|
+
<a name="related" />
|
21
35
|
## Related Projects
|
22
36
|
|
23
37
|
* [Grape](https://github.com/ruby-grape/grape)
|
24
38
|
* [Swagger UI](https://github.com/wordnik/swagger-ui)
|
25
39
|
|
40
|
+
|
41
|
+
<a name="swagger-spec" />
|
26
42
|
## Swagger-Spec
|
27
43
|
|
28
|
-
Grape-swagger generates documentation per [Swagger Spec
|
44
|
+
Grape-swagger generates documentation per [Swagger Spec 2.0](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md).
|
45
|
+
|
29
46
|
|
47
|
+
<a name="install" />
|
30
48
|
## Installation
|
31
49
|
|
32
50
|
Add to your Gemfile:
|
33
51
|
|
34
|
-
```
|
52
|
+
```ruby
|
53
|
+
gem 'grape-swagger', git: 'git@github.com:ruby-grape/grape-swagger.git'
|
54
|
+
```
|
55
|
+
|
35
56
|
|
36
57
|
## Upgrade
|
37
58
|
|
38
59
|
Please see [UPGRADING](UPGRADING.md) when upgrading from a previous version.
|
39
60
|
|
61
|
+
|
62
|
+
<a name="usage" />
|
40
63
|
## Usage
|
41
64
|
|
42
65
|
Mount all your different APIs (with ```Grape::API``` superclass) on a root node. In the root class definition, include ```add_swagger_documentation```, this sets up the system and registers the documentation on '/swagger_doc'. See [example/api.rb](example/api.rb) for a simple demo.
|
43
66
|
|
44
67
|
|
45
|
-
```
|
68
|
+
```ruby
|
46
69
|
require 'grape-swagger'
|
47
70
|
|
48
71
|
module API
|
49
72
|
class Root < Grape::API
|
73
|
+
format :json
|
50
74
|
mount API::Cats
|
51
75
|
mount API::Dogs
|
52
76
|
mount API::Pirates
|
@@ -57,11 +81,12 @@ end
|
|
57
81
|
|
58
82
|
To explore your API, either download [Swagger UI](https://github.com/wordnik/swagger-ui) and set it up yourself or go to the [online swagger demo](http://petstore.swagger.wordnik.com/) and enter your localhost url documentation root in the url field (probably something in the line of http://localhost:3000/swagger_doc).
|
59
83
|
|
84
|
+
|
60
85
|
### CORS
|
61
86
|
|
62
87
|
If you use the online demo, make sure your API supports foreign requests by enabling CORS in Grape, otherwise you'll see the API description, but requests on the API won't return. Use [rack-cors](https://github.com/cyu/rack-cors) to enable CORS.
|
63
88
|
|
64
|
-
````
|
89
|
+
````ruby
|
65
90
|
require 'rack/cors'
|
66
91
|
use Rack::Cors do
|
67
92
|
allow do
|
@@ -73,103 +98,181 @@ end
|
|
73
98
|
|
74
99
|
Alternatively you can set CORS headers in a Grape `before` block.
|
75
100
|
|
76
|
-
```
|
101
|
+
```ruby
|
77
102
|
before do
|
78
103
|
header['Access-Control-Allow-Origin'] = '*'
|
79
104
|
header['Access-Control-Request-Method'] = '*'
|
80
105
|
end
|
81
106
|
````
|
82
107
|
|
83
|
-
## Configure
|
84
108
|
|
85
|
-
|
109
|
+
<a name="configure" />
|
110
|
+
## Configure
|
86
111
|
|
87
|
-
|
112
|
+
[host](#host)
|
113
|
+
[base_path](#base_path)
|
114
|
+
[mount_path](#mount_path)
|
115
|
+
[add_base_path](#add_base_path)
|
116
|
+
[add_version](#add_version)
|
117
|
+
[markdown](#markdown)
|
118
|
+
[api_version](#api_version)
|
119
|
+
[models](#models)
|
120
|
+
[hide_documentation_path](#hide_documentation_path)
|
121
|
+
[info](#info)
|
88
122
|
|
89
|
-
The API class to document, default `self`.
|
90
123
|
|
91
|
-
|
124
|
+
You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
|
92
125
|
|
93
|
-
|
126
|
+
*not all configuration options supported yet*, but is WIP
|
94
127
|
|
95
|
-
#### class_name
|
96
128
|
|
97
|
-
|
129
|
+
`host` and `base_path` are also accepting a `proc` to evaluate.
|
98
130
|
|
99
|
-
|
131
|
+
<a name="host" />
|
132
|
+
#### host:
|
133
|
+
Sets explicit the `host`
|
134
|
+
```ruby
|
135
|
+
add_swagger_documentation \
|
136
|
+
host: 'www.no-example.com'
|
137
|
+
```
|
100
138
|
|
101
|
-
|
139
|
+
<a name="base_path" />
|
140
|
+
#### base_path:
|
141
|
+
Base path of the API that's being exposed.
|
142
|
+
```ruby
|
143
|
+
add_swagger_documentation \
|
144
|
+
base_path: '/super/api'
|
145
|
+
```
|
102
146
|
|
103
|
-
|
147
|
+
<a name="mount_path" />
|
148
|
+
#### mount_path:
|
149
|
+
The path where the API documentation is loaded, default is `/swagger_doc`.
|
150
|
+
```ruby
|
151
|
+
add_swagger_documentation \
|
152
|
+
mount_path: '/docu'
|
153
|
+
```
|
104
154
|
|
105
|
-
|
155
|
+
#### add_base_path:
|
156
|
+
Add `basePath` key to the JSON documentation, default is `false`.
|
157
|
+
```ruby
|
158
|
+
add_swagger_documentation \
|
159
|
+
add_base_path: true
|
160
|
+
```
|
106
161
|
|
107
|
-
####
|
162
|
+
#### add_version:
|
163
|
+
Add `version` key to the JSON documentation, default is `true`.
|
164
|
+
```ruby
|
165
|
+
add_swagger_documentation \
|
166
|
+
add_version: false
|
167
|
+
```
|
108
168
|
|
109
|
-
Don't add `.(format)` to the end of URLs, default is `false`.
|
110
169
|
|
111
|
-
|
170
|
+
<a name="markdown" />
|
171
|
+
#### markdown:
|
172
|
+
Allow markdown in `detail`, default is `false`. (disabled) See [below](#md_usage) for details.
|
112
173
|
|
113
|
-
|
174
|
+
```ruby
|
175
|
+
add_swagger_documentation \
|
176
|
+
markdown: GrapeSwagger::Markdown::KramdownAdapter.new
|
177
|
+
```
|
178
|
+
or alternative
|
179
|
+
```ruby
|
180
|
+
add_swagger_documentation \
|
181
|
+
markdown: GrapeSwagger::Markdown::RedcarpetAdapter.new
|
182
|
+
```
|
114
183
|
|
115
|
-
|
184
|
+
<a name="api_version" />
|
185
|
+
#### api_version:
|
186
|
+
```ruby
|
187
|
+
add_swagger_documentation \
|
188
|
+
api_version: 'v1'
|
189
|
+
```
|
116
190
|
|
117
|
-
|
191
|
+
Version of the API that's being exposed.
|
118
192
|
|
119
|
-
#### authorizations
|
120
193
|
|
194
|
+
#### *authorizations*:
|
121
195
|
This value is added to the `authorizations` key in the JSON documentation.
|
122
196
|
|
123
|
-
#### root_base_path
|
124
197
|
|
125
|
-
Add `basePath` key to the JSON documentation, default is `true`.
|
126
198
|
|
127
|
-
|
199
|
+
<a name="models" />
|
200
|
+
#### models:
|
201
|
+
A list of entities to document. Combine with the [grape-entity](https://github.com/ruby-grape/grape-entity) gem.
|
128
202
|
|
129
|
-
|
203
|
+
These would be added to the definitions section of the swagger file.
|
130
204
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
205
|
+
```ruby
|
206
|
+
add_swagger_documentation \
|
207
|
+
models: [
|
208
|
+
TheApi::Entities::UseResponse,
|
209
|
+
TheApi::Entities::ApiError
|
210
|
+
]
|
211
|
+
```
|
136
212
|
|
137
|
-
|
213
|
+
<a name="hide_documentation_path" />
|
214
|
+
#### hide_documentation_path: (default: `true`)
|
215
|
+
```ruby
|
216
|
+
add_swagger_documentation \
|
217
|
+
hide_documentation_path: true
|
218
|
+
```
|
138
219
|
|
139
|
-
|
220
|
+
Don't show the `/swagger_doc` path in the generated swagger documentation.
|
140
221
|
|
141
|
-
|
222
|
+
<a name="info" />
|
223
|
+
#### info:
|
224
|
+
```ruby
|
225
|
+
add_swagger_documentation \
|
226
|
+
info: {
|
227
|
+
title: "The API title to be displayed on the API homepage.",
|
228
|
+
description: "A description of the API.",
|
229
|
+
contact_name: "Contact name",
|
230
|
+
contact_email: "Contact@email.com",
|
231
|
+
contact_url: "Contact URL",
|
232
|
+
license: "The name of the license.",
|
233
|
+
license_url: "www.The-URL-of-the-license.org",
|
234
|
+
terms_of_service_url: "www.The-URL-of-the-terms-and-service.com",
|
235
|
+
}
|
236
|
+
```
|
142
237
|
|
143
|
-
|
144
|
-
* `:description`: A description of the API.
|
145
|
-
* `:contact`: Contact email.
|
146
|
-
* `:license`: The name of the license.
|
147
|
-
* `:license_url`: The URL of the license.
|
148
|
-
* `:terms_of_service_url`: The URL of the API terms and conditions.
|
238
|
+
A hash merged into the `info` key of the JSON documentation.
|
149
239
|
|
150
|
-
#### api_documentation
|
151
240
|
|
241
|
+
<!-- #### *api_documentation*:
|
152
242
|
Customize the Swagger API documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description".
|
153
243
|
|
154
|
-
```
|
244
|
+
```ruby
|
155
245
|
add_swagger_documentation \
|
156
246
|
api_documentation: { desc: 'Reticulated splines API swagger-compatible documentation.' }
|
157
247
|
```
|
158
248
|
|
159
|
-
#### specific_api_documentation
|
160
249
|
|
250
|
+
#### *specific_api_documentation*:
|
161
251
|
Customize the Swagger API specific documentation route, typically contains a `desc` field. The default description is "Swagger compatible API description for specific API".
|
162
252
|
|
163
|
-
```
|
253
|
+
```ruby
|
164
254
|
add_swagger_documentation \
|
165
255
|
specific_api_documentation: { desc: 'Reticulated splines API swagger-compatible endpoint documentation.' }
|
166
|
-
```
|
256
|
+
``` -->
|
257
|
+
|
258
|
+
|
259
|
+
<a name="routes" />
|
260
|
+
## Routes Configuration
|
167
261
|
|
168
|
-
|
262
|
+
[Swagger Header Parameters](#headers)
|
263
|
+
[Hiding an Endpoint](#hiding)
|
264
|
+
[Defining an endpoint as array](#array)
|
265
|
+
[Using an options hash](#options)
|
266
|
+
[Specify endpoint details](#details)
|
267
|
+
[Response documentation](#response)
|
268
|
+
|
269
|
+
|
270
|
+
<a name="headers" />
|
271
|
+
#### Swagger Header Parameters <a name="headers" />
|
169
272
|
|
170
273
|
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.
|
171
274
|
|
172
|
-
```
|
275
|
+
```ruby
|
173
276
|
desc "Return super-secret information", {
|
174
277
|
headers: {
|
175
278
|
"XAuthToken" => {
|
@@ -184,69 +287,82 @@ desc "Return super-secret information", {
|
|
184
287
|
}
|
185
288
|
```
|
186
289
|
|
187
|
-
|
290
|
+
|
291
|
+
<a name="hiding" />
|
292
|
+
#### Hiding an Endpoint
|
188
293
|
|
189
294
|
You can hide an endpoint by adding ```hidden: true``` in the description of the endpoint:
|
190
295
|
|
191
|
-
```
|
296
|
+
```ruby
|
192
297
|
desc 'Hide this endpoint', hidden: true
|
193
298
|
```
|
194
299
|
|
195
300
|
Endpoints can be conditionally hidden by providing a callable object such as a lambda which evaluates to the desired
|
196
301
|
state:
|
197
302
|
|
198
|
-
```
|
303
|
+
```ruby
|
199
304
|
desc 'Conditionally hide this endpoint', hidden: lambda { ENV['EXPERIMENTAL'] != 'true' }
|
200
305
|
```
|
201
306
|
|
202
|
-
## Overriding Auto-Generated Nicknames
|
203
307
|
|
204
|
-
|
308
|
+
#### Overriding Auto-Generated Nicknames
|
205
309
|
|
206
|
-
```
|
310
|
+
You can specify a swagger nickname to use instead of the auto generated name by adding `:nickname 'string'``` in the description of the endpoint.
|
311
|
+
|
312
|
+
```ruby
|
207
313
|
desc 'Get a full list of pets', nickname: 'getAllPets'
|
208
314
|
```
|
209
315
|
|
210
|
-
|
316
|
+
|
317
|
+
<a name="array" />
|
318
|
+
#### Defining an endpoint as array
|
211
319
|
|
212
320
|
You can define an endpoint as array by adding `is_array` in the description:
|
213
321
|
|
214
|
-
```
|
322
|
+
```ruby
|
215
323
|
desc 'Get a full list of pets', is_array: true
|
216
324
|
```
|
217
325
|
|
218
|
-
|
326
|
+
|
327
|
+
<a name="options" />
|
328
|
+
#### Using an options hash
|
219
329
|
|
220
330
|
The Grape DSL supports either an options hash or a restricted block to pass settings. Passing the `nickname`, `hidden` and `is_array` options together with response codes is only possible when passing an options hash.
|
221
331
|
Since the syntax differs you'll need to adjust it accordingly:
|
222
332
|
|
223
|
-
```
|
333
|
+
```ruby
|
224
334
|
desc 'Get all kittens!', {
|
225
|
-
:
|
226
|
-
:
|
227
|
-
:
|
228
|
-
:
|
229
|
-
:
|
335
|
+
hidden: true,
|
336
|
+
is_array: true,
|
337
|
+
nickname: 'getKittens',
|
338
|
+
entity: Entities::Kitten, # or success
|
339
|
+
http_codes: [[401, 'KittenBitesError', Entities::BadKitten]] # or failure
|
340
|
+
# also explicit as hash: [{ code: 401, mssage: 'KittenBitesError', model: Entities::BadKitten }]
|
341
|
+
produces: [ "array", "of", "mime_types" ],
|
342
|
+
consumes: [ "array", "of", "mime_types" ]
|
230
343
|
}
|
231
344
|
get '/kittens' do
|
232
345
|
```
|
233
346
|
|
234
|
-
|
347
|
+
|
348
|
+
<a name="details" />
|
349
|
+
#### Specify endpoint details
|
235
350
|
|
236
351
|
To specify further details for an endpoint, use the `detail` option within a block passed to `desc`:
|
237
352
|
|
238
|
-
```
|
353
|
+
```ruby
|
239
354
|
desc 'Get all kittens!' do
|
240
355
|
detail 'this will expose all the kittens'
|
241
356
|
end
|
242
357
|
get '/kittens' do
|
243
358
|
```
|
244
359
|
|
245
|
-
|
360
|
+
|
361
|
+
#### Overriding param type
|
246
362
|
|
247
363
|
You can override paramType in POST|PUT methods to query, using the documentation hash.
|
248
364
|
|
249
|
-
```
|
365
|
+
```ruby
|
250
366
|
params do
|
251
367
|
requires :action, type: Symbol, values: [:PAUSE, :RESUME, :STOP], documentation: { param_type: 'query' }
|
252
368
|
end
|
@@ -255,69 +371,88 @@ post :act do
|
|
255
371
|
end
|
256
372
|
```
|
257
373
|
|
258
|
-
|
374
|
+
|
375
|
+
#### Expose nested namespace as standalone route
|
376
|
+
|
259
377
|
Use the `nested: false` property in the `swagger` option to make nested namespaces appear as standalone resources.
|
260
378
|
This option can help to structure and keep the swagger schema simple.
|
261
379
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
380
|
+
```ruby
|
381
|
+
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false } do
|
382
|
+
get :order_id do
|
383
|
+
...
|
384
|
+
end
|
385
|
+
end
|
386
|
+
```
|
267
387
|
|
268
388
|
All routes that belong to this namespace (here: the `GET /order_id`) will then be assigned to the `store_order` route instead of the `store` resource route.
|
269
389
|
|
270
|
-
It is also possible to expose a
|
271
|
-
|
272
|
-
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false } do
|
273
|
-
get :order_id do
|
274
|
-
...
|
275
|
-
end
|
276
|
-
namespace 'actions', desc: 'Order actions' do, nested: false
|
277
|
-
get 'evaluate' do
|
278
|
-
...
|
279
|
-
end
|
280
|
-
end
|
281
|
-
end
|
390
|
+
It is also possible to expose a namespace within another already exposed namespace:
|
282
391
|
|
392
|
+
```ruby
|
393
|
+
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false } do
|
394
|
+
get :order_id do
|
395
|
+
...
|
396
|
+
end
|
397
|
+
namespace 'actions', desc: 'Order actions' do, nested: false
|
398
|
+
get 'evaluate' do
|
399
|
+
...
|
400
|
+
end
|
401
|
+
end
|
402
|
+
end
|
403
|
+
```
|
283
404
|
Here, the `GET /order_id` appears as operation of the `store_order` resource and the `GET /evaluate` as operation of the `store_orders_actions` route.
|
284
405
|
|
285
|
-
|
406
|
+
|
407
|
+
##### With a custom name
|
408
|
+
|
286
409
|
Auto generated names for the standalone version of complex nested resource do not have a nice look.
|
287
410
|
You can set a custom name with the `name` property inside the `swagger` option, but only if the namespace gets exposed as standalone route.
|
288
411
|
The name should not contain whitespaces or any other special characters due to further issues within swagger-ui.
|
289
412
|
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
413
|
+
```ruby
|
414
|
+
namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false, name: 'Store-orders' } do
|
415
|
+
get :order_id do
|
416
|
+
...
|
417
|
+
end
|
418
|
+
end
|
419
|
+
```
|
295
420
|
|
421
|
+
|
422
|
+
<a name="additions" />
|
296
423
|
## Additional documentation
|
297
424
|
|
298
|
-
|
425
|
+
|
426
|
+
[Markdown in Detail](#md_usage)
|
427
|
+
[Response documentation](#response)
|
428
|
+
|
429
|
+
|
430
|
+
### Setting a Swagger defaultValue
|
299
431
|
|
300
432
|
Grape allows for an additional documentation hash to be passed to a parameter.
|
301
433
|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
434
|
+
```ruby
|
435
|
+
params do
|
436
|
+
requires :id, type: Integer, desc: 'Coffee ID'
|
437
|
+
requires :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', documentation: { example: 72 }
|
438
|
+
end
|
439
|
+
```
|
306
440
|
|
307
441
|
The example parameter will populate the Swagger UI with the example value, and can be used for optional or required parameters.
|
308
442
|
|
309
443
|
Grape uses the option `default` to set a default value for optional parameters. This is different in that Grape will set your parameter to the provided default if the parameter is omitted, whereas the example value above will only set the value in the UI itself. This will set the Swagger `defaultValue` to the provided value. Note that the example value will override the Grape default value.
|
310
444
|
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
445
|
+
```ruby
|
446
|
+
params do
|
447
|
+
requires :id, type: Integer, desc: 'Coffee ID'
|
448
|
+
optional :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', default: 72
|
449
|
+
end
|
450
|
+
```
|
316
451
|
|
317
452
|
|
318
|
-
|
453
|
+
### Grape Entities
|
319
454
|
|
320
|
-
Add the [grape-entity](https://github.com/
|
455
|
+
Add the [grape-entity](https://github.com/agileanimal/grape-entity) gem to our Gemfile.
|
321
456
|
|
322
457
|
The following example exposes statuses. And exposes statuses documentation adding :type and :desc.
|
323
458
|
|
@@ -331,10 +466,6 @@ module API
|
|
331
466
|
end
|
332
467
|
|
333
468
|
class Link < Grape::Entity
|
334
|
-
def self.entity_name
|
335
|
-
'link'
|
336
|
-
end
|
337
|
-
|
338
469
|
expose :href, documentation: { type: 'url' }
|
339
470
|
expose :rel, documentation: { type: 'string'}
|
340
471
|
end
|
@@ -343,14 +474,17 @@ module API
|
|
343
474
|
class Statuses < Grape::API
|
344
475
|
version 'v1'
|
345
476
|
|
346
|
-
desc 'Statuses index',
|
477
|
+
desc 'Statuses index',
|
478
|
+
entity: API::Entities::Status
|
347
479
|
get '/statuses' do
|
348
480
|
statuses = Status.all
|
349
481
|
type = current_user.admin? ? :full : :default
|
350
482
|
present statuses, with: API::Entities::Status, type: type
|
351
483
|
end
|
352
484
|
|
353
|
-
desc 'Creates a new status',
|
485
|
+
desc 'Creates a new status',
|
486
|
+
entity: API::Entities::Status,
|
487
|
+
params: API::Entities::Status.documentation
|
354
488
|
post '/statuses' do
|
355
489
|
...
|
356
490
|
end
|
@@ -358,11 +492,13 @@ module API
|
|
358
492
|
end
|
359
493
|
```
|
360
494
|
|
361
|
-
|
495
|
+
|
496
|
+
#### Relationships
|
362
497
|
|
363
498
|
You may safely omit `type` from relationships, as it can be inferred. However, if you need to specify or override it, use the full name of the class leaving out any modules named `Entities` or `Entity`.
|
364
499
|
|
365
|
-
|
500
|
+
|
501
|
+
##### 1xN
|
366
502
|
|
367
503
|
```ruby
|
368
504
|
module API
|
@@ -391,7 +527,8 @@ module API
|
|
391
527
|
end
|
392
528
|
```
|
393
529
|
|
394
|
-
|
530
|
+
|
531
|
+
##### 1x1
|
395
532
|
|
396
533
|
Note: `is_array` is `false` by default.
|
397
534
|
|
@@ -422,63 +559,47 @@ module API
|
|
422
559
|
end
|
423
560
|
```
|
424
561
|
|
425
|
-
## Markdown in Detail/Notes
|
426
562
|
|
427
|
-
|
563
|
+
<a name="md_usage" />
|
564
|
+
### Markdown in Detail
|
565
|
+
|
566
|
+
The grape-swagger gem allows you to add an explanation in markdown in the detail field. Which would result in proper formatted markdown in Swagger UI.
|
428
567
|
Grape-swagger uses adapters for several markdown formatters. It includes adapters for [kramdown](http://kramdown.rubyforge.org) (kramdown [syntax](http://kramdown.rubyforge.org/syntax.html)) and [redcarpet](https://github.com/vmg/redcarpet).
|
429
568
|
The adapters are packed in the GrapeSwagger::Markdown modules. We do not include the markdown gems in our gemfile, so be sure to include or install the depended gems.
|
430
569
|
|
570
|
+
To use it, add a new instance of the adapter to the markdown options of `add_swagger_documentation`, such as:
|
571
|
+
```ruby
|
572
|
+
add_swagger_documentation \
|
573
|
+
markdown: GrapeSwagger::Markdown::KramdownAdapter.new(options)
|
574
|
+
```
|
575
|
+
and write your route details in GFM, examples could be find in [details spec](blob/master/spec/swagger_v2/api_swagger_v2_detail_spec.rb)
|
431
576
|
|
432
|
-
|
577
|
+
|
578
|
+
#### Kramdown
|
433
579
|
If you want to use kramdown as markdown formatter, you need to add kramdown to your gemfile.
|
434
580
|
|
435
|
-
```
|
581
|
+
```ruby
|
436
582
|
gem 'kramdown'
|
437
583
|
```
|
438
584
|
|
439
585
|
Configure your api documentation route with:
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
add_swagger_documentation(
|
444
|
-
markdown: GrapeSwagger::Markdown::KramdownAdapter
|
445
|
-
)
|
586
|
+
```ruby
|
587
|
+
add_swagger_documentation \
|
588
|
+
markdown: GrapeSwagger::Markdown::KramdownAdapter.new(options)
|
446
589
|
```
|
447
590
|
|
448
|
-
Finally you can write endpoint descriptions the with markdown enabled.
|
449
|
-
|
450
|
-
``` ruby
|
451
|
-
desc "Reserve a burger in heaven" do
|
452
|
-
detail <<-NOTE
|
453
|
-
Veggie Burgers in Heaven
|
454
|
-
-----------------
|
455
|
-
|
456
|
-
> A veggie burger doesn't come for free
|
457
|
-
|
458
|
-
If you want to reserve a veggie burger in heaven, you have to do
|
459
|
-
some crazy stuff on earth.
|
460
591
|
|
461
|
-
|
462
|
-
puts 'help people'
|
463
|
-
end
|
464
|
-
|
465
|
-
* _Will go to Heaven:_ Probably
|
466
|
-
* _Will go to Hell:_ Probably not
|
467
|
-
NOTE
|
468
|
-
end
|
469
|
-
```
|
470
|
-
|
471
|
-
### Redcarpet
|
592
|
+
#### Redcarpet
|
472
593
|
As alternative you can use [redcarpet](https://github.com/vmg/redcarpet) as formatter, you need to include redcarpet in your gemspec. If you also want to use [rouge](https://github.com/jneen/rouge) as syntax highlighter you also need to include it.
|
473
594
|
|
474
|
-
```
|
595
|
+
```ruby
|
475
596
|
gem 'redcarpet'
|
476
597
|
gem 'rouge'
|
477
598
|
```
|
478
599
|
|
479
600
|
Configure your api documentation route with:
|
480
601
|
|
481
|
-
```
|
602
|
+
```ruby
|
482
603
|
add_swagger_documentation(
|
483
604
|
markdown: GrapeSwagger::Markdown::RedcarpetAdapter.new(render_options: { highlighter: :rouge })
|
484
605
|
)
|
@@ -486,13 +607,16 @@ add_swagger_documentation(
|
|
486
607
|
|
487
608
|
Alternatively you can disable rouge by adding `:none` as highlighter option. You can add redcarpet extensions and render options trough the `extenstions:` and `render_options:` parameters.
|
488
609
|
|
489
|
-
|
610
|
+
|
611
|
+
#### Custom markdown formatter
|
612
|
+
|
490
613
|
You can also add your custom adapter for your favourite markdown formatter, as long it responds to the method `markdown(text)` and it formats the given text.
|
491
614
|
|
492
|
-
|
615
|
+
|
616
|
+
```ruby
|
493
617
|
module API
|
494
618
|
|
495
|
-
class
|
619
|
+
class FancyAdapter
|
496
620
|
attr_reader :adapter
|
497
621
|
|
498
622
|
def initialize(options)
|
@@ -505,212 +629,175 @@ module API
|
|
505
629
|
end
|
506
630
|
end
|
507
631
|
|
508
|
-
add_swagger_documentation markdown:
|
632
|
+
add_swagger_documentation markdown: FancyAdapter.new(no_links: true)
|
509
633
|
end
|
510
|
-
|
511
634
|
```
|
512
635
|
|
636
|
+
|
637
|
+
<a name="response" />
|
513
638
|
## Response documentation
|
514
639
|
|
515
|
-
You can also document the HTTP status codes with a description and a specified model that your API returns with the following syntax.
|
640
|
+
You can also document the HTTP status codes with a description and a specified model, as ref in the schema to the definitions, that your API returns with one of the following syntax.
|
516
641
|
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
642
|
+
In the following cases, the schema ref would be taken from route.
|
643
|
+
|
644
|
+
```ruby
|
645
|
+
desc 'thing', http_codes: [ { code: 400, message: "Invalid parameter entry" } ]
|
646
|
+
get '/thing' do
|
522
647
|
...
|
523
648
|
end
|
524
649
|
```
|
525
650
|
|
526
|
-
|
651
|
+
```ruby
|
652
|
+
desc 'thing' do
|
653
|
+
params Entities::Something.documentation
|
654
|
+
http_codes [ { code: 400, message: "Invalid parameter entry" } ]
|
655
|
+
end
|
656
|
+
get '/thing' do
|
657
|
+
...
|
658
|
+
end
|
659
|
+
```
|
527
660
|
|
528
|
-
|
661
|
+
```ruby
|
662
|
+
get '/thing', http_codes: [
|
663
|
+
{ code: 200, message: 'Ok' },
|
664
|
+
{ code: 400, message: "Invalid parameter entry" }
|
665
|
+
] do
|
666
|
+
...
|
667
|
+
end
|
668
|
+
```
|
529
669
|
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
670
|
+
By adding a `model` key, e.g. this would be taken.
|
671
|
+
```ruby
|
672
|
+
get '/thing', http_codes: [
|
673
|
+
{ code: 200, message: 'Ok' },
|
674
|
+
{ code: 422, message: "Invalid parameter entry", model: Entities::ApiError }
|
675
|
+
] do
|
676
|
+
...
|
677
|
+
end
|
678
|
+
```
|
679
|
+
If no status code is defined [defaults](/lib/grape-swagger/endpoint.rb#L121) would be taken.
|
535
680
|
|
536
|
-
|
537
|
-
set [`i18n_scope`](#i18n_scope) to a custom scope (or an array of scopes) when calling
|
538
|
-
`add_swagger_documentation`.
|
681
|
+
The result is then something like following:
|
539
682
|
|
540
|
-
|
683
|
+
```json
|
684
|
+
"responses": {
|
685
|
+
"200": {
|
686
|
+
"description": "get Horses",
|
687
|
+
"schema": {
|
688
|
+
"$ref": "#/definitions/Thing"
|
689
|
+
}
|
690
|
+
},
|
691
|
+
"401": {
|
692
|
+
"description": "HorsesOutError",
|
693
|
+
"schema": {
|
694
|
+
"$ref": "#/definitions/ApiError"
|
695
|
+
}
|
696
|
+
}
|
697
|
+
},
|
698
|
+
```
|
541
699
|
|
542
|
-
|
543
|
-
|
544
|
-
it will use the message specified in the API code, then default to blank.
|
700
|
+
<a name="extensions" />
|
701
|
+
## Extensions
|
545
702
|
|
546
|
-
|
703
|
+
Swagger spec2.0 supports extensions on different levels, for the moment,
|
704
|
+
the documentation on `verb`, `path` and `definition` level would be supported.
|
705
|
+
The documented key would be generated from the `x` + `-` + key of the submitted hash,
|
706
|
+
for possibilities refer to the [extensions spec](spec/lib/extensions_spec.rb).
|
707
|
+
To get an overview *how* the extensions would be defined on grape level, see the following examples:
|
547
708
|
|
709
|
+
- `verb` extension, add a `x` key to the `desc` hash:
|
548
710
|
```ruby
|
549
|
-
desc '
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
get
|
556
|
-
|
711
|
+
desc 'This returns something with extension on verb level',
|
712
|
+
x: { some: 'stuff' }
|
713
|
+
```
|
714
|
+
this would generate:
|
715
|
+
```json
|
716
|
+
"/path":{
|
717
|
+
"get":{
|
718
|
+
"…":"…",
|
719
|
+
"x-some":"stuff"
|
720
|
+
}
|
721
|
+
}
|
557
722
|
```
|
558
723
|
|
559
|
-
|
724
|
+
- `path` extension, by setting via route settings:
|
725
|
+
```ruby
|
726
|
+
route_setting :x_path, { some: 'stuff' }
|
727
|
+
```
|
728
|
+
this would generate:
|
729
|
+
```json
|
730
|
+
"/path":{
|
731
|
+
"x-some":"stuff",
|
732
|
+
"get":{
|
733
|
+
"…":"…",
|
734
|
+
}
|
735
|
+
}
|
736
|
+
```
|
560
737
|
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
738
|
+
- `definition` extension, again by setting via route settings,
|
739
|
+
here the status code must be provided, for which definition the extensions should be:
|
740
|
+
```ruby
|
741
|
+
route_setting :x_def, { for: 422, other: 'stuff' }
|
742
|
+
```
|
743
|
+
this would generate:
|
744
|
+
```json
|
745
|
+
"/definitions":{
|
746
|
+
"ApiError":{
|
747
|
+
"x-other":"stuff",
|
748
|
+
"…":"…",
|
749
|
+
}
|
750
|
+
}
|
751
|
+
```
|
752
|
+
or, for more definitions:
|
753
|
+
```ruby
|
754
|
+
route_setting :x_def, [{ for: 422, other: 'stuff' }, { for: 200, some: 'stuff' }]
|
568
755
|
```
|
569
756
|
|
570
|
-
|
571
|
-
|
572
|
-
message - "This will return a full list of kittens, and you can change the way of sorting them.".
|
573
|
-
Parameter `:sort` will have a description from locale file too.
|
757
|
+
<a="example" />
|
758
|
+
# Example
|
574
759
|
|
575
|
-
|
760
|
+
Go into example directory and run it: `$ bundle exec rackup`
|
761
|
+
go to: `http://localhost:9292/swagger_doc` to get it
|
576
762
|
|
577
|
-
|
763
|
+
For request examples load the [postman file]()
|
764
|
+
## Grouping the API list using Namespace
|
578
765
|
|
579
|
-
|
766
|
+
Use namespace for grouping APIs
|
580
767
|
|
581
|
-
|
768
|
+
![grape-swagger-v2-new-corrected](https://cloud.githubusercontent.com/assets/1027590/13516020/979cfefa-e1f9-11e5-9624-f4a6b17a3c8a.png)
|
582
769
|
|
583
|
-
|
584
|
-
* `info.desc` or `info.description` for `:description` field.
|
585
|
-
* `info.contact` for `:contact` field.
|
586
|
-
* `info.license` for `:license` field.
|
587
|
-
* `info.license_url` for `:license_url` field.
|
588
|
-
* `info.terms_of_service_url` for `:terms_of_service_url` field.
|
770
|
+
# Example
|
589
771
|
|
590
|
-
|
772
|
+
```ruby
|
773
|
+
class NamespaceApi < Grape::API
|
774
|
+
namespace :hudson do
|
775
|
+
desc 'Document root'
|
776
|
+
get '/' do
|
777
|
+
end
|
778
|
+
end
|
591
779
|
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
namespace itself is available as a variable for interpolation.
|
780
|
+
namespace :hudson do
|
781
|
+
desc 'This gets something.',
|
782
|
+
notes: '_test_'
|
596
783
|
|
597
|
-
|
784
|
+
get '/simple' do
|
785
|
+
{ bla: 'something' }
|
786
|
+
end
|
787
|
+
end
|
598
788
|
|
599
|
-
|
600
|
-
|
601
|
-
|
789
|
+
namespace :colorado do
|
790
|
+
desc 'This gets something for URL using - separator.',
|
791
|
+
notes: '_test_'
|
602
792
|
|
603
|
-
|
604
|
-
|
605
|
-
route_params :id do
|
606
|
-
get :'password/strength' do
|
793
|
+
get '/simple-test' do
|
794
|
+
{ bla: 'something' }
|
607
795
|
end
|
608
796
|
end
|
609
797
|
end
|
610
|
-
|
798
|
+
…
|
611
799
|
|
612
|
-
|
613
|
-
the key of this endpoint.
|
614
|
-
|
615
|
-
So an endpoint's description message can be given under `<endpoint_key>.desc` or
|
616
|
-
`<endpoint_key>.description`. And use `<endpoint_key>.detail` or `<endpoint_key>.notes` for the
|
617
|
-
message of its further details.
|
618
|
-
|
619
|
-
#### Params Description
|
620
|
-
|
621
|
-
The first default key of endpoint parameter's description translation is
|
622
|
-
`<endpoint_key>.params.<param_name>`. But considering that some endpoints could usually share a
|
623
|
-
same parameter, it will be a little annoyed to duplicate its description message everywhere. So if
|
624
|
-
no translation found in the first default key, grape-swagger will try to find all its parent keys.
|
625
|
-
Take above endpoint for example, it may have a parameter named `:id`, then the lookup keys are:
|
626
|
-
|
627
|
-
1. `users.:id.password.strength.get.params.id`
|
628
|
-
1. `users.:id.password.strength.params.id`
|
629
|
-
1. `users.:id.password.params.id`
|
630
|
-
1. `users.:id.params.id`
|
631
|
-
1. `users.params.id`
|
632
|
-
1. `params.id`
|
633
|
-
|
634
|
-
#### Models/Entities Description
|
635
|
-
|
636
|
-
A model class' lookup key is by default its class name, without module part, and underscored. Each
|
637
|
-
property's key is then `entities.<class_key>.<property_name>`. When not found, grape-swagger will
|
638
|
-
try to check the its ancestors, up to a class whose name is `Entity`.
|
639
|
-
|
640
|
-
Say if there is model class `User` inherits `Grape::Entity`, and another model `AdminUser` inherits
|
641
|
-
`User`, to translate a property named `:email` of `AdminUser`, the lookup keys are:
|
642
|
-
|
643
|
-
1. `entities.admin_user.email`
|
644
|
-
1. `entities.user.email`
|
645
|
-
1. `entities.default.email`
|
646
|
-
|
647
|
-
#### Example Locale File
|
648
|
-
|
649
|
-
```yaml
|
650
|
-
en:
|
651
|
-
api:
|
652
|
-
info:
|
653
|
-
title: My Awesome API
|
654
|
-
desc: Some detail information about this API.
|
655
|
-
entities:
|
656
|
-
default:
|
657
|
-
id: Resource identifier
|
658
|
-
user:
|
659
|
-
name: User's real name
|
660
|
-
email: User's login email address
|
661
|
-
sign_up_at: When the user signed up
|
662
|
-
admin_user:
|
663
|
-
level: Which level the admin is
|
664
|
-
password_strength:
|
665
|
-
level: A 0~4 integer indicates `very_weak` to `strong`
|
666
|
-
crack_time: An estimated time for force cracking the password, in seconds
|
667
|
-
params:
|
668
|
-
locale: Used to change locale of endpoint's responding message
|
669
|
-
sort: To specify the order of result list, default is %{default_sort}
|
670
|
-
users:
|
671
|
-
desc: Operations about not-disabled users
|
672
|
-
get:
|
673
|
-
desc: Gets a list of users
|
674
|
-
detail: You can control how to sort the results.
|
675
|
-
':id':
|
676
|
-
params:
|
677
|
-
id: User id
|
678
|
-
get:
|
679
|
-
desc: Finds user by id
|
680
|
-
email:
|
681
|
-
put:
|
682
|
-
desc: Changes a user's email
|
683
|
-
params:
|
684
|
-
email: A new email
|
685
|
-
password:
|
686
|
-
strength:
|
687
|
-
get:
|
688
|
-
desc: Gets the strength estimation of a user's password
|
689
|
-
detail: The estimation is done by a well-known algorithm when he changed his password
|
690
|
-
swagger_doc:
|
691
|
-
desc: Endpoints for API documents
|
692
|
-
get:
|
693
|
-
desc: Gets root API document
|
694
|
-
':name':
|
695
|
-
get:
|
696
|
-
desc: Gets specific resource API document
|
697
|
-
params:
|
698
|
-
name: Resource name
|
699
|
-
```
|
700
|
-
|
701
|
-
### Customization
|
702
|
-
|
703
|
-
The translation can be customized by using different kind/format of message in source code.
|
704
|
-
|
705
|
-
* A string or `nil`: It will become the default message when a translation is not defined.
|
706
|
-
* A symbol: Looks up translation using the symbol as a key, but will fallback to default key(s).
|
707
|
-
* A hash with the following keys:
|
708
|
-
* key: A symbol, defines custom lookup key.
|
709
|
-
* default: A string, defines the default message when translation can not be found.
|
710
|
-
* translate: A boolean (default is `true`), set to `false` to skip translation for this message.
|
711
|
-
* scope: A symbol, or a string, or an array of them. Used to replace the `i18n_scope` config
|
712
|
-
value for a custom lookup scope.
|
713
|
-
* Any other key/value will be sent to the translation function for interpolation.
|
800
|
+
```
|
714
801
|
|
715
802
|
## Contributing to grape-swagger
|
716
803
|
|