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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -1
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +14 -22
  5. data/.travis.yml +7 -4
  6. data/CHANGELOG.md +53 -26
  7. data/Gemfile +1 -1
  8. data/README.md +414 -327
  9. data/RELEASING.md +3 -4
  10. data/example/api/endpoints.rb +132 -0
  11. data/example/api/entities.rb +18 -0
  12. data/example/config.ru +36 -2
  13. data/example/example_requests.postman_collection +146 -0
  14. data/example/swagger-example.png +0 -0
  15. data/grape-swagger.gemspec +9 -6
  16. data/lib/grape-swagger.rb +69 -99
  17. data/lib/grape-swagger/doc_methods.rb +69 -544
  18. data/lib/grape-swagger/doc_methods/data_type.rb +77 -0
  19. data/lib/grape-swagger/doc_methods/extensions.rb +75 -0
  20. data/lib/grape-swagger/doc_methods/move_params.rb +153 -0
  21. data/lib/grape-swagger/doc_methods/operation_id.rb +27 -0
  22. data/lib/grape-swagger/doc_methods/optional_object.rb +15 -0
  23. data/lib/grape-swagger/doc_methods/parse_params.rb +113 -0
  24. data/lib/grape-swagger/doc_methods/path_string.rb +29 -0
  25. data/lib/grape-swagger/doc_methods/produces_consumes.rb +12 -0
  26. data/lib/grape-swagger/doc_methods/status_codes.rb +17 -0
  27. data/lib/grape-swagger/doc_methods/tag_name_description.rb +26 -0
  28. data/lib/grape-swagger/endpoint.rb +317 -0
  29. data/lib/grape-swagger/version.rb +1 -1
  30. data/spec/lib/data_type_spec.rb +57 -0
  31. data/spec/lib/endpoint_spec.rb +6 -0
  32. data/spec/lib/extensions_spec.rb +127 -0
  33. data/spec/lib/move_params_spec.rb +298 -0
  34. data/spec/lib/operation_id_spec.rb +24 -0
  35. data/spec/lib/optional_object_spec.rb +40 -0
  36. data/spec/lib/path_string_spec.rb +38 -0
  37. data/spec/lib/produces_consumes_spec.rb +98 -0
  38. data/spec/markdown/kramdown_adapter_spec.rb +2 -9
  39. data/spec/markdown/redcarpet_adapter_spec.rb +2 -16
  40. data/spec/spec_helper.rb +7 -13
  41. data/spec/support/api_swagger_v2_result.rb +204 -0
  42. data/spec/support/namespace_tags.rb +73 -0
  43. data/spec/support/the_api_entities.rb +52 -0
  44. data/spec/support/the_paths_definitions.rb +94 -0
  45. data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +32 -0
  46. data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +151 -0
  47. data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +109 -0
  48. data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +124 -0
  49. data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +51 -0
  50. data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +44 -0
  51. data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +56 -0
  52. data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +146 -0
  53. data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +197 -0
  54. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +151 -0
  55. data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +217 -0
  56. data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +64 -0
  57. data/spec/swagger_v2/api_swagger_v2_response_spec.rb +184 -0
  58. data/spec/swagger_v2/api_swagger_v2_spec.rb +207 -0
  59. data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +121 -0
  60. data/spec/{boolean_params_spec.rb → swagger_v2/boolean_params_spec.rb} +2 -2
  61. data/spec/{default_api_spec.rb → swagger_v2/default_api_spec.rb} +40 -36
  62. data/spec/swagger_v2/description_not_initialized.rb +39 -0
  63. data/spec/{float_api_spec.rb → swagger_v2/float_api_spec.rb} +2 -2
  64. data/spec/{form_params_spec.rb → swagger_v2/form_params_spec.rb} +9 -9
  65. data/spec/{grape-swagger_spec.rb → swagger_v2/grape-swagger_spec.rb} +0 -0
  66. data/spec/swagger_v2/hide_api_spec.rb +131 -0
  67. data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
  68. data/spec/swagger_v2/namespace_tags_prefix_spec.rb +84 -0
  69. data/spec/swagger_v2/namespace_tags_spec.rb +76 -0
  70. data/spec/{namespaced_api_spec.rb → swagger_v2/namespaced_api_spec.rb} +6 -26
  71. data/spec/{param_type_spec.rb → swagger_v2/param_type_spec.rb} +10 -8
  72. data/spec/{param_values_spec.rb → swagger_v2/param_values_spec.rb} +52 -22
  73. data/spec/swagger_v2/params_array_spec.rb +63 -0
  74. data/spec/swagger_v2/params_hash_spec.rb +65 -0
  75. data/spec/swagger_v2/params_nested_spec.rb +63 -0
  76. data/spec/{reference_entity.rb → swagger_v2/reference_entity.rb} +18 -23
  77. data/spec/swagger_v2/response_model_spec.rb +212 -0
  78. data/spec/swagger_v2/simple_mounted_api_spec.rb +264 -0
  79. metadata +175 -90
  80. data/example/api.rb +0 -66
  81. data/lib/grape-swagger/markdown.rb +0 -23
  82. data/spec/api_description_spec.rb +0 -43
  83. data/spec/api_global_models_spec.rb +0 -77
  84. data/spec/api_models_spec.rb +0 -364
  85. data/spec/api_paths_spec.rb +0 -128
  86. data/spec/api_root_spec.rb +0 -30
  87. data/spec/api_with_nil_types.rb +0 -50
  88. data/spec/api_with_path_versioning_spec.rb +0 -33
  89. data/spec/api_with_prefix_and_namespace_spec.rb +0 -32
  90. data/spec/api_with_standalone_namespace_spec.rb +0 -215
  91. data/spec/array_entity_spec.rb +0 -34
  92. data/spec/array_params_spec.rb +0 -85
  93. data/spec/grape-swagger_helper_spec.rb +0 -152
  94. data/spec/group_params_spec.rb +0 -31
  95. data/spec/hash_params_spec.rb +0 -30
  96. data/spec/hide_api_spec.rb +0 -124
  97. data/spec/i18n_spec.rb +0 -364
  98. data/spec/markdown/markdown_spec.rb +0 -27
  99. data/spec/mounted_target_class_spec.rb +0 -63
  100. data/spec/mutually_exclusive_spec.rb +0 -36
  101. data/spec/non_default_api_spec.rb +0 -733
  102. data/spec/response_model_spec.rb +0 -121
  103. data/spec/simple_mounted_api_spec.rb +0 -213
  104. data/spec/support/i18n_helper.rb +0 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d4e52b2b16e5431c3ef42aa544c85b084c003cf9
4
- data.tar.gz: 86d13f947c54e91c207d58fda741b47f3cddd231
3
+ metadata.gz: 881259fe2427869fbd1dbe2af3925eff5c6ec991
4
+ data.tar.gz: 6557a2cee402f5f5fd8f4d03ca07b4abcb423531
5
5
  SHA512:
6
- metadata.gz: f96ee66b108a5880309103e38fc2137bdbb3e993c679c192d943fdec6a467a57008babc39c4379c2a9f6c77e5da93d80f8c98b0996a35d09d4117a1355b13d37
7
- data.tar.gz: 6041bc57d9612d87ccc5acf9c38089703835b3fa3f5a78233e63fec91866e5a82973da24c22cd78cf0b77758116562c5f3f7258cb48e0b0d23e5ea54d308ff46
6
+ metadata.gz: ecc8ed62a66df220e26243a208270fedbf80b91af6b5b26a8d5bb4f99486cdfd14cdd721929c0340dabffa739d49142b27102d89ef9081c0ff1ac5098bc616a6
7
+ data.tar.gz: 4bbf258316ad6e4ee1a56b5016e86150f29a63eb71047a943975979c60330b86e5f17e9e3554afc76f676d5a72b0af363efc09e25324113bb0d7b06c9cb656fc
data/.gitignore CHANGED
@@ -30,4 +30,11 @@ Gemfile.lock
30
30
  # For MacOS:
31
31
  #
32
32
  .DS_Store
33
- .project
33
+ .project
34
+ example.json
35
+ swagger_spec1.2/
36
+ ToDo.md
37
+ .ruby-version
38
+ spec/params_entity_spec.rb
39
+ vendor/bundle/
40
+ spec/swagger_v2/x-dummy.rb
data/.rubocop.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  AllCops:
2
2
  Exclude:
3
3
  - vendor/**/*
4
+ - spec/**/*
5
+ - swagger_spec1.2/**/*
6
+ - example/**/*
4
7
 
5
8
  inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml CHANGED
@@ -1,42 +1,42 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-04-09 20:38:17 -0400 using RuboCop version 0.33.0.
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: 14
9
+ # Offense count: 10
10
10
  Metrics/AbcSize:
11
- Max: 222
11
+ Max: 176
12
12
 
13
13
  # Offense count: 1
14
14
  # Configuration parameters: CountComments.
15
15
  Metrics/ClassLength:
16
- Max: 149
16
+ Max: 250
17
17
 
18
- # Offense count: 7
18
+ # Offense count: 6
19
19
  Metrics/CyclomaticComplexity:
20
- Max: 42
20
+ Max: 38
21
21
 
22
- # Offense count: 359
22
+ # Offense count: 304
23
23
  # Configuration parameters: AllowURI, URISchemes.
24
24
  Metrics/LineLength:
25
- Max: 206
25
+ Max: 242
26
26
 
27
- # Offense count: 24
27
+ # Offense count: 21
28
28
  # Configuration parameters: CountComments.
29
29
  Metrics/MethodLength:
30
- Max: 175
30
+ Max: 139
31
31
 
32
32
  # Offense count: 1
33
33
  # Configuration parameters: CountComments.
34
34
  Metrics/ModuleLength:
35
- Max: 478
35
+ Max: 200
36
36
 
37
- # Offense count: 5
37
+ # Offense count: 4
38
38
  Metrics/PerceivedComplexity:
39
- Max: 44
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: 96
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.11.0 (Next)
1
+ ### 0.20.0 / 2016-04-09
2
2
 
3
3
  #### Features
4
4
 
5
- * [#368](https://github.com/ruby-grape/grape-swagger/pull/368): Requires Grape 0.16.2, fixes `route_xxx` deprecation messages - [@dblock](https://github.com/dblock).
5
+ [#371](https://github.com/ruby-grape/grape-swagger/pull/371)
6
6
 
7
- ### 0.10.5 (2016-04-12)
7
+ - adds param type `body` handling
8
8
 
9
- * [#338](https://github.com/ruby-grape/grape-swagger/pull/338): Fixed handling of nested Array parameters - [@itoufo](https://github.com/itoufo).
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
- #### Fixes
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
- * [#322](https://github.com/ruby-grape/grape-swagger/pull/322): Entity's `entity_name` takes predence over root - [@gekola](https://github.com/gekola).
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
- ### 0.10.4 (December 7, 2015)
42
+ * Namespace based tagging help the swagger GUI to group the API list
18
43
 
19
- * [#315](https://github.com/ruby-grape/grape-swagger/pull/315): Require `grape-entity` < 0.5.0 - [@dblock](https://github.com/dblock).
44
+ ### 0.10.3 (Next)
20
45
 
21
- ### 0.10.3 (December 7, 2015)
46
+ [#336](https://github.com/ruby-grape/grape-swagger/pull/336) changes of swagger-2.0 fork, to support it
22
47
 
23
- * [#292](https://github.com/ruby-grape/grape-swagger/pull/292): Support i18n - [@calfzhou](https://github.com/calfzhou).
24
- * [#297](https://github.com/ruby-grape/grape-swagger/pull/297): Correct use of documentation param_type - [@fab-girard](https://github.com/fab-girard).
25
- * [#305](https://github.com/ruby-grape/grape-swagger/pull/305): Speedup by parsing models smarter, not harder - [@jhollinger](https://github.com/jhollinger).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
167
- * Added support for file uploads, and proper `paramType` - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
173
- * Adapted to rails plugin structure - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
174
- * Allowed cross origin, so swagger can be used from official site - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
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 - [@tim-vandecasteele](https://github.com/tim-vandecasteele).
205
+ * Initial public release - [@ruby-grape](https://github.com/ruby-grape).
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- case version = ENV['GRAPE_VERSION'] || '~> 0.16.2'
5
+ case version = ENV['GRAPE_VERSION'] || '~> 0.14.0'
6
6
  when 'HEAD'
7
7
  gem 'grape', github: 'ruby-grape/grape'
8
8
  else
data/README.md CHANGED
@@ -1,52 +1,76 @@
1
- **these branch generates a swagger file, which is conform to the 1.2 spec**
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
- compatible versions:
3
+ ##### Table of Contents
4
4
 
5
- - `grape <~ 0.14.0`
6
- - `grape-entity < 0.5.0`
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
- # grape-swagger
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-1.2)](https://travis-ci.org/ruby-grape/grape-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/splines.png)
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 1.2](https://github.com/swagger-api/swagger-spec/blob/master/versions/1.2.md).
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
- ```gem 'grape-swagger'```
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
- ``` ruby
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
- ```` ruby
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
- ``` ruby
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
- You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
109
+ <a name="configure" />
110
+ ## Configure
86
111
 
87
- #### target_class
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
- #### mount_path
124
+ You can pass a hash with optional configuration settings to ```add_swagger_documentation```.
92
125
 
93
- The path where the API documentation is loaded, default is `/swagger_doc`.
126
+ *not all configuration options supported yet*, but is WIP
94
127
 
95
- #### class_name
96
128
 
97
- API class name.
129
+ `host` and `base_path` are also accepting a `proc` to evaluate.
98
130
 
99
- #### markdown
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
- Allow markdown in `detail`/`notes`, default is `nil`. (disabled) See below for details.
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
- #### i18n_scope
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
- Translations scope (or array of scopes) default is `:api`. [See below for details](#i18n).
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
- #### hide_format
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
- #### api_version
170
+ <a name="markdown" />
171
+ #### markdown:
172
+ Allow markdown in `detail`, default is `false`. (disabled) See [below](#md_usage) for details.
112
173
 
113
- Version of the API that's being exposed.
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
- #### base_path
184
+ <a name="api_version" />
185
+ #### api_version:
186
+ ```ruby
187
+ add_swagger_documentation \
188
+ api_version: 'v1'
189
+ ```
116
190
 
117
- Base path of the API that's being exposed. This configuration parameter accepts a `proc` to evaluate `base_path`, useful when you need to use request attributes to determine its value.
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
- #### models
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
- A list of entities to document. Combine with the [grape-entity](https://github.com/ruby-grape/grape-entity) gem. See below for details.
203
+ These would be added to the definitions section of the swagger file.
130
204
 
131
- #### hide_documentation_path
132
-
133
- Don't show the `/swagger_doc` path in the generated swagger documentation.
134
-
135
- #### format
205
+ ```ruby
206
+ add_swagger_documentation \
207
+ models: [
208
+ TheApi::Entities::UseResponse,
209
+ TheApi::Entities::ApiError
210
+ ]
211
+ ```
136
212
 
137
- Documentation response format, default is `:json`.
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
- #### info
220
+ Don't show the `/swagger_doc` path in the generated swagger documentation.
140
221
 
141
- A hash merged into the `info` key of the JSON documentation. This may contain:
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
- * `:title`: The API title to be displayed on the API homepage.
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
- ``` ruby
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
- ``` ruby
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
- ## Swagger Header Parameters
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
- ``` ruby
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
- ## Hiding an Endpoint
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
- ``` ruby
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
- ``` ruby
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
- You can specify a swagger nickname to use instead of the auto generated name by adding `:nickname 'string'` in the description of the endpoint.
308
+ #### Overriding Auto-Generated Nicknames
205
309
 
206
- ``` ruby
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
- ## Defining an endpoint as array
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
- ``` ruby
322
+ ```ruby
215
323
  desc 'Get a full list of pets', is_array: true
216
324
  ```
217
325
 
218
- ## Using an options hash
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
- ``` ruby
333
+ ```ruby
224
334
  desc 'Get all kittens!', {
225
- :hidden => true,
226
- :is_array => true,
227
- :nickname => 'getKittens',
228
- :entity => Entities::Kitten, # use entity instead of success
229
- :http_codes => [[401, 'KittenBitesError', Entities::BadKitten]] # use http_codes instead of failure
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
- ## Specify endpoint details
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
- ``` ruby
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
- ## Overriding param type
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
- ``` ruby
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
- ## Expose nested namespace as standalone route
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
- namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false } do
263
- get :order_id do
264
- ...
265
- end
266
- end
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 namspace within another already exposed namespace:
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
- ### With a custom name
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
- namespace 'store/order', desc: 'Order operations within a store', swagger: { nested: false, name: 'Store-orders' } do
291
- get :order_id do
292
- ...
293
- end
294
- end
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
- ## Setting a Swagger defaultValue
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
- params do
303
- requires :id, type: Integer, desc: 'Coffee ID'
304
- requires :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', documentation: { example: 72 }
305
- end
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
- params do
313
- requires :id, type: Integer, desc: 'Coffee ID'
314
- optional :temperature, type: Integer, desc: 'Temperature of the coffee in celcius', default: 72
315
- end
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
- ## Grape Entities
453
+ ### Grape Entities
319
454
 
320
- Add the [grape-entity](https://github.com/ruby-grape/grape-entity) gem to our Gemfile.
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', entity: API::Entities::Status
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', entity: API::Entities::Status, params: API::Entities::Status.documentation
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
- ### Relationships
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
- #### 1xN
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
- #### 1x1
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
- The grape-swagger gem allows you to add an explanation in markdown in the detail/notes field. Which would result in proper formatted markdown in Swagger UI.
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
- ### Kramdown
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
- ``` ruby
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
- def do_good
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
- ``` ruby
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
- ### Custom markdown formatter
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
- ``` ruby
615
+
616
+ ```ruby
493
617
  module API
494
618
 
495
- class MySuperbMarkdownFormatterAdapter
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: MySuperbMarkdownFormatterAdapter.new(no_links: true)
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
- ``` ruby
518
- get '/', http_codes: [
519
- [200, 'Ok', Entities::Client],
520
- [400, "Invalid parameter entry"]
521
- ] do
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
- ## I18n
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
- Grape-swagger supports I18n for most messages of the JSON documentation, including:
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
- * API [info](#info)
531
- * Namespaces/resources description
532
- * Endpoints description and detail/notes
533
- * Params (including headers) description
534
- * Models/entities description
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
- By default, grape-swagger will lookup translations inside `:api` scope. This can be configured by
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
- ### Translation and Default Message
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
- To localize your API document, you can add a locale file containing translated messages.
543
- Grape-swagger will try to look up translation for each message. If a translation cannot be found,
544
- it will use the message specified in the API code, then default to blank.
700
+ <a name="extensions" />
701
+ ## Extensions
545
702
 
546
- Take the following sample API for example:
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 'Gets all kittens' do
550
- detail 'This will expose all the kittens.'
551
- end
552
- params do
553
- optional :sort
554
- end
555
- get :kittens do
556
- end
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
- To generate I18n documentation, you can add a locale file with translated messages:
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
- ```yaml
562
- api:
563
- kittens:
564
- get:
565
- detail: This will return a full list of kittens, and you can change the way of sorting them.
566
- params:
567
- sort: Specifies the order of results
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
- The endpoint description in generated API document will be "Gets all kittens" - specified in source
571
- code - because there is no translation defined. And endpoint details will be overwritten by locale
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
- ### Default Lookup Keys
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
- The following lookup keys are all within the scope given by `i18n_scope`.
763
+ For request examples load the [postman file]()
764
+ ## Grouping the API list using Namespace
578
765
 
579
- #### API Info
766
+ Use namespace for grouping APIs
580
767
 
581
- The default lookup key of a field in API info is `info.<field>`, i.e.:
768
+ ![grape-swagger-v2-new-corrected](https://cloud.githubusercontent.com/assets/1027590/13516020/979cfefa-e1f9-11e5-9624-f4a6b17a3c8a.png)
582
769
 
583
- * `info.title` for `:title` field.
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
- #### Namespaces/Resources Description
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
- Grape-swagger will give each root namespace a default description, such as "*Operations about
593
- users*", where `users` is a namespace (pluralized). But you can change it by provide your own
594
- message under key `<namespace>.desc` or `<namespace>.description` in your locale file. And the
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
- ### Endpoints Description and Detail/Notes
784
+ get '/simple' do
785
+ { bla: 'something' }
786
+ end
787
+ end
598
788
 
599
- An endpoint's default lookup key is in format of `<namespace>.<path>.<http_method>`. And if there
600
- are nested namespaces or multi-level path, all parts will be join by `.`. For example for this
601
- endpoint:
789
+ namespace :colorado do
790
+ desc 'This gets something for URL using - separator.',
791
+ notes: '_test_'
602
792
 
603
- ```ruby
604
- namespace :users do
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
- Its corresponding lookup key will be `users.:id.password.strength.get`, let's say that this is
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