swaggard 1.5.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +84 -131
  3. data/app/views/swaggard/swagger/index.html.erb +51 -53
  4. data/lib/swaggard/api_definition.rb +42 -28
  5. data/lib/swaggard/configuration.rb +1 -5
  6. data/lib/swaggard/engine.rb +0 -10
  7. data/lib/swaggard/parsers/routes.rb +1 -1
  8. data/lib/swaggard/swagger/operation.rb +32 -12
  9. data/lib/swaggard/swagger/parameters/base.rb +7 -6
  10. data/lib/swaggard/swagger/parameters/body.rb +9 -9
  11. data/lib/swaggard/swagger/parameters/form.rb +10 -1
  12. data/lib/swaggard/swagger/parameters/list.rb +6 -8
  13. data/lib/swaggard/swagger/parameters/query.rb +9 -9
  14. data/lib/swaggard/swagger/response.rb +15 -8
  15. data/lib/swaggard/swagger/response_header.rb +1 -1
  16. data/lib/swaggard/swagger/type.rb +2 -2
  17. data/lib/swaggard/version.rb +1 -1
  18. data/lib/swaggard.rb +9 -1
  19. data/spec/fixtures/api.json +1 -1
  20. data/{app/assets/stylesheets/swaggard/typography.css.scss → spec/fixtures/dummy/app/assets/config/manifest.js} +0 -0
  21. data/spec/fixtures/openapi_3_1_schema.json +1440 -0
  22. data/spec/integration/openapi_spec.rb +28 -0
  23. metadata +16 -52
  24. data/app/assets/fonts/swaggard/DroidSans-Bold.ttf +0 -0
  25. data/app/assets/fonts/swaggard/DroidSans.ttf +0 -0
  26. data/app/assets/javascripts/swaggard/application.js +0 -28
  27. data/app/assets/javascripts/swaggard/lang/ca.js +0 -53
  28. data/app/assets/javascripts/swaggard/lang/el.js +0 -56
  29. data/app/assets/javascripts/swaggard/lang/en.js +0 -56
  30. data/app/assets/javascripts/swaggard/lang/es.js +0 -53
  31. data/app/assets/javascripts/swaggard/lang/fr.js +0 -54
  32. data/app/assets/javascripts/swaggard/lang/geo.js +0 -56
  33. data/app/assets/javascripts/swaggard/lang/it.js +0 -52
  34. data/app/assets/javascripts/swaggard/lang/ja.js +0 -56
  35. data/app/assets/javascripts/swaggard/lang/ko-kr.js +0 -53
  36. data/app/assets/javascripts/swaggard/lang/pl.js +0 -53
  37. data/app/assets/javascripts/swaggard/lang/pt.js +0 -53
  38. data/app/assets/javascripts/swaggard/lang/ru.js +0 -56
  39. data/app/assets/javascripts/swaggard/lang/tr.js +0 -53
  40. data/app/assets/javascripts/swaggard/lang/zh-cn.js +0 -56
  41. data/app/assets/javascripts/swaggard/lib/backbone-min.js +0 -1
  42. data/app/assets/javascripts/swaggard/lib/handlebars-4.0.5.js +0 -3
  43. data/app/assets/javascripts/swaggard/lib/highlight.9.1.0.pack.js +0 -1
  44. data/app/assets/javascripts/swaggard/lib/highlight.9.1.0.pack_extended.js +0 -1
  45. data/app/assets/javascripts/swaggard/lib/jquery-1.8.0.min.js +0 -3
  46. data/app/assets/javascripts/swaggard/lib/jquery.ba-bbq.min.js +0 -1
  47. data/app/assets/javascripts/swaggard/lib/jquery.slideto.min.js +0 -1
  48. data/app/assets/javascripts/swaggard/lib/jquery.wiggle.min.js +0 -1
  49. data/app/assets/javascripts/swaggard/lib/jsoneditor.min.js +0 -5
  50. data/app/assets/javascripts/swaggard/lib/lodash.min.js +0 -2
  51. data/app/assets/javascripts/swaggard/lib/marked.js +0 -1
  52. data/app/assets/javascripts/swaggard/lib/object-assign-pollyfill.js +0 -1
  53. data/app/assets/javascripts/swaggard/lib/swagger-oauth.js +0 -1
  54. data/app/assets/javascripts/swaggard/swaggard.js +0 -72
  55. data/app/assets/javascripts/swaggard/swagger-ui.js +0 -25378
  56. data/app/assets/javascripts/swaggard/translator.js +0 -39
  57. data/app/assets/stylesheets/swaggard/application.css +0 -16
  58. data/app/assets/stylesheets/swaggard/application_print.css +0 -15
  59. data/app/assets/stylesheets/swaggard/print.css.scss +0 -4
  60. data/app/assets/stylesheets/swaggard/reset.css +0 -1
  61. data/app/assets/stylesheets/swaggard/screen.css.scss +0 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c06259c5d8dbe6e19b9b430db34b7195f9343d3be21d9e1d589137938db110a
4
- data.tar.gz: 6a8e26c725b2b4e34acead3be8656396ebe116e3be87a8ed8ab431486c059212
3
+ metadata.gz: c18dab011ccdb176dd6324a815a0138f051d85cb9383a60681a857c886d516a5
4
+ data.tar.gz: b8bd9f253e75c56c5308bae421f92cd12b54457edc40aa3d2857b1ac15d1764a
5
5
  SHA512:
6
- metadata.gz: 348fa3f26b48cd2a50c84b88fd13177d9e77566cf07df3f1dfca3f15866166dfb54f0585fd4d6f2389391999902dbee91db2ecf6a696d6e96286c127aea9d9ad
7
- data.tar.gz: '049bca86afdacadf166b46245c550e9c95ac9d4d41c923bf862e6544a47f7e0f9be79c9e14bc9bd849a6fd56adb81f55974a594aff83b45cb1d4e0d39d6c2e62'
6
+ metadata.gz: a8406ac609bb110a3cb6d67647e5d775db2d293f0ab754020920442a86f202b80064da43843e616144f084ec9d93694b4bd086119f92413c055bd68d80266bb0
7
+ data.tar.gz: 5caa69ecc7b014dccca548a27cdad88428c30f423ff2d922104fef04d0861b7f083af89cd7c0aec21d5f9a52e4173bd822cfb5757cb7bfe245100e3f84481d27
data/README.md CHANGED
@@ -1,24 +1,23 @@
1
1
  # Swaggard
2
2
 
3
- Swaggard is a Rails Engine that can be used to document a REST api. It does this by generating a json that is compliant with [Swagger](http://swagger.io) and displaying it using [Swagger-ui](https://github.com/wordnik/swagger-ui).
3
+ Swaggard is a Rails Engine that documents a REST API by generating a JSON file compliant with [OpenAPI 3.1](https://spec.openapis.org/oas/v3.1.0) and displaying it using [Swagger UI 5](https://github.com/swagger-api/swagger-ui).
4
4
  This gem is inspired and based on [SwaggerYard](https://github.com/synctv/swagger_yard) by [Chris Trinh](https://github.com/chtrinh).
5
5
 
6
6
 
7
7
  ## Compatibility
8
8
 
9
- This table tracks the version of Swagger UI used on each Swaggard version and
10
- the supported rails version.
11
-
12
- Swaggard Version | Swagger UI Version | Supported Rails Versions
13
- ---------------- | ------------------- | ------------------------
14
- 1.1.x | 2.2.8 | 4 - 6
15
- 1.0.x | 2.2.8 | 4 - 5
16
- 0.5.x | 2.2.8 | 4 - 5
17
- 0.4.x | 2.2.8 | 4
18
- 0.3.x | 2.1.3 | 4
19
- 0.2.x | 2.1.3 | 4
20
- 0.1.x | 2.1.3 | 4
21
- 0.0.x | 2.1.8-M1 | 4
9
+ Swaggard Version | OpenAPI Version | Swagger UI Version | Supported Rails Versions
10
+ ---------------- | --------------- | ------------------- | ------------------------
11
+ 4.0.x | 3.1 | 5.32.0 | 7 - 8
12
+ 2.0.x | 2.0 | 2.2.8 | 7 - 8
13
+ 1.1.x | 2.0 | 2.2.8 | 4 - 6
14
+ 1.0.x | 2.0 | 2.2.8 | 4 - 5
15
+ 0.5.x | 2.0 | 2.2.8 | 4 - 5
16
+ 0.4.x | 2.0 | 2.2.8 | 4
17
+ 0.3.x | 2.0 | 2.1.3 | 4
18
+ 0.2.x | 2.0 | 2.1.3 | 4
19
+ 0.1.x | 2.0 | 2.1.3 | 4
20
+ 0.0.x | 2.0 | 2.1.8-M1 | 4
22
21
 
23
22
 
24
23
  ## Installation
@@ -31,47 +30,35 @@ Install the gem with Bundler:
31
30
 
32
31
  bundle install
33
32
 
33
+
34
34
  ## Getting Started
35
35
 
36
- First generate a Swaggard configuration initializer file.
36
+ Generate a Swaggard configuration initializer:
37
37
 
38
38
  rails g swaggard:install
39
39
 
40
- This will install the file `swaggard.rb` to your Rails `config/initializers` directory which you can then alter the config.
41
-
42
- Mount your engine
43
-
44
- # config/routes.rb
45
-
46
- mount Swaggard::Engine, at: '/api_docs/swagger/'
47
-
48
- Make sure the asset pipeline is enabled by either requiring all railties
49
- or just the sprockets one:
50
-
51
- # config/application.rb
40
+ This installs `config/initializers/swaggard.rb` which you can edit to configure the gem.
52
41
 
53
- require 'sprockets/railtie'
42
+ Mount the engine:
54
43
 
55
- Access your service documentation
44
+ # config/routes.rb
45
+ mount Swaggard::Engine, at: '/api_docs/swagger/'
56
46
 
57
- open http://localhost:3000/api_docs/swagger/
47
+ Access the documentation UI:
58
48
 
49
+ open http://localhost:3000/api_docs/swagger/
59
50
 
60
- Access the raw swagger json
51
+ Access the raw OpenAPI JSON:
61
52
 
62
- open http://localhost:3000/api_docs/swagger.json
53
+ open http://localhost:3000/api_docs/swagger.json
63
54
 
64
55
 
65
56
  ## Example
66
57
 
67
58
  By just using Swaggard you'll get documentation for the endpoints that exist on your service:
68
- http method, path, path params. And grouping will be done based on the controller that holds
69
- each path.
70
-
71
- This is fine base but you should add more documentation in order to provide more information
72
- of the expected inputs and outputs or even change the grouping of the endpoints.
59
+ HTTP method, path, and path parameters. Grouping is done based on the controller that holds each path.
73
60
 
74
- Here is a example of how to use Swaggard
61
+ Add YARD comments to provide richer documentation:
75
62
 
76
63
  # config/routes.rb
77
64
 
@@ -145,40 +132,43 @@ Here is a example of how to use Swaggard
145
132
 
146
133
  end
147
134
 
148
- Will generate
149
-
150
- ![Web UI](https://raw.githubusercontent.com/Moove-it/swaggard/master/example/web-ui.png)
151
135
 
152
-
153
- ## Available tags
136
+ ## Available Tags
154
137
 
155
138
  ### Controllers
156
139
 
157
- - `@tag name` This is used to indicate under what `name` tag this controller needs to be grouped. If not provided and Con.ignore_untagged_controllers = true it will use the full controller class name.
158
- - `@query_parameter [type] name` This is used to indicate that your action receives the `name` parameter of `type` on the query string.
159
- - `@body_parameter [type] name` This is used to indicate that your action receives the `name` parameter of `type` on the body.
160
- - `@response_class type` This is used to indicate that your action returns a response of `type`.
161
- - `@response_status 200` This is used to indicate the response code of your action when everything goes well.
162
- - `@response_root name` This is used to indicate that your action returns its response inside a root key element `name`. If not provided the root is omitted and the response its returned directly.
163
- - `@form_parameter`
164
- - `@parameter_list`
165
-
166
- If you want to generate documentation for all controllers even for those that don't have a tag do:
167
- # config/initializers/swaggard.rb
140
+ - `@tag name` Group this controller under `name`. Defaults to the controller path if `ignore_untagged_controllers` is false.
141
+ - `@query_parameter [type] name` Query string parameter.
142
+ - `@body_parameter [type] name` Request body property. Generates a `requestBody` in the OpenAPI output.
143
+ - `@form_parameter [type] name` Form data parameter (`application/x-www-form-urlencoded`).
144
+ - `@parameter_list` Enum-style query parameter list.
145
+ - `@response_class type` Response schema type. Supports `Array<Type>` for array responses.
146
+ - `@response_status 200` — HTTP response status code.
147
+ - `@response_root name` — Wrap the response in a root key.
148
+ - `@response_description text` — Description for the response.
149
+ - `@response_example path/to/example.json` Example response. Inlined if the file exists on disk, otherwise used as an `externalValue` URI.
150
+ - `@response_header [type] name description` — Response header.
151
+ - `@operation_id id` — Override the `operationId`.
152
+ - `@body_required` — Mark the request body as required.
153
+ - `@body_title title` — Title for the generated request body schema.
154
+ - `@body_definition SchemaName` — Reference an existing schema by name as the request body.
155
+
156
+ To document all controllers including those without a `@tag`:
157
+
168
158
  Swaggard.configure do |config|
169
- ...
170
159
  config.ignore_untagged_controllers = false
171
- ...
172
160
  end
173
161
 
174
162
 
175
163
  ### Models
176
164
 
177
- - `@attr [type] name` This is used to indicate that your models has an attribute `name` of `type`.
165
+ - `@attr [type] name` Model attribute.
166
+ - `@ignore_inherited` — Do not inherit properties from parent class.
167
+
178
168
 
179
169
  ## Primitives
180
170
 
181
- When one of these types is given Swaggard will handle them directly instead of searching for a definition:
171
+ When one of these types is used Swaggard maps it to an inline schema instead of a `$ref`:
182
172
 
183
173
  - integer
184
174
  - long
@@ -194,128 +184,92 @@ When one of these types is given Swaggard will handle them directly instead of s
194
184
  - hash
195
185
 
196
186
 
197
- ## Authentication
198
-
199
- Swaggard supports two types of authentication: header and query.
187
+ ## Custom Types
200
188
 
201
- You can configure it as follows:
189
+ Define reusable object schemas that are placed in `components/schemas` and referenced via `$ref`:
202
190
 
203
- # config/initializers/swaggard.rb
204
191
  Swaggard.configure do |config|
205
- config.authentication_type = 'header' # Defaults to 'query'
206
- config.authentication_key = 'X-AUTH-TOKEN' # Defaults to 'api_key'
207
- config.authentication_value = 'your-secret-key' # Initial value for authentication. Defaults to ''
192
+ config.add_custom_type('Address', type: :object,
193
+ properties: {
194
+ line1: { type: :string },
195
+ city: { type: :string },
196
+ state: { type: :string },
197
+ zip: { type: :string },
198
+ },
199
+ required: %i[line1 city state zip])
208
200
  end
209
201
 
210
- Even if you provide a authentication_value you can later change it from the ui.
211
202
 
203
+ ## Authentication
212
204
 
213
- ## Access authorization
214
-
215
- Swaggard supports access authorization.
216
-
217
- You can configure it as follows:
205
+ Swaggard supports injecting auth credentials into every request via `requestInterceptor`.
218
206
 
219
- # config/initializers/swaggard.rb
220
207
  Swaggard.configure do |config|
221
- config.access_username = 'admin'
222
- config.access_password = 'password'
208
+ config.authentication_type = 'header' # or 'query'. Defaults to 'query'
209
+ config.authentication_key = 'X-AUTH-TOKEN' # Defaults to 'api_key'
210
+ config.authentication_value = 'your-secret-key' # Defaults to ''
223
211
  end
224
212
 
225
- If you not set `access_username`, everyone will have access to Swagger documentation.
226
-
227
-
228
- ## Additional parameters
213
+ For OAuth2 / API key schemes visible in the Swagger UI Authorize dialog, use `add_security_definition` and `add_security`:
229
214
 
230
- Swaggard supports additional parameters to be sent on every request, either as a header or as part of the query.
231
-
232
- You can configure it as follows:
233
-
234
- # config/initializers/swaggard.rb
235
215
  Swaggard.configure do |config|
236
- config.additional_parameters = [{ key: 'TOKEN', type: 'header', value: '234' }]
216
+ config.add_security_definition('api_key', type: 'apiKey', name: 'Authorization', in: 'header')
217
+ config.add_security('api_key')
237
218
  end
238
219
 
239
- type can be 'header' or 'query'.
240
- If value is provided then it will be used as a default.
241
- You can change/set the value for the parameters in the ui.
242
-
243
220
 
244
- ## Default content type
221
+ ## Access Authorization
245
222
 
246
- You can set default content type in Swaggard configuration as follows:
223
+ Protect the documentation UI with HTTP Basic auth:
247
224
 
248
- # config/initializers/swaggard.rb
249
225
  Swaggard.configure do |config|
250
- config.default_content_type = 'application/json'
226
+ config.access_username = 'admin'
227
+ config.access_password = 'password'
251
228
  end
252
229
 
253
- If you set `default_content_type`, Swagger will use it in example request.
230
+ If `access_username` is not set, the UI is publicly accessible.
254
231
 
255
232
 
256
- ## Language
233
+ ## Additional Parameters
257
234
 
258
- You can set the language for SwaggerUI as follows:
235
+ Inject extra headers or query parameters into every request:
259
236
 
260
- # config/initializers/swaggard.rb
261
237
  Swaggard.configure do |config|
262
- config.language = 'es'
238
+ config.additional_parameters = [{ key: 'STORE-CODE', type: 'header', value: '1' }]
263
239
  end
264
240
 
265
- The default value is: 'en'.
266
- Supported values are:
267
- - ca
268
- - el
269
- - en
270
- - es
271
- - fr
272
- - geo
273
- - it
274
- - ja
275
- - ko-kr
276
- - pl
277
- - pt
278
- - ru
279
- - tr
280
- - zh-cn
241
+ `type` can be `'header'` or `'query'`.
281
242
 
282
243
 
283
244
  ## Caching
284
245
 
285
- You can improve Swagger performance by using caching. You can enable `use_cache` in Swaggard configuration as follows:
246
+ Cache the generated OpenAPI JSON for better performance:
286
247
 
287
- # config/initializers/swaggard.rb
288
248
  Swaggard.configure do |config|
289
249
  config.use_cache = Rails.env.production?
290
250
  end
291
251
 
292
- If you set `use_cache` as `Rails.env.production?`, Swagger will use cache only in production mode.
252
+ Clear the cache with:
293
253
 
294
- Note. For cache clearing you can execute `rake swaggard:clear_cache`.
254
+ rake swaggard:clear_cache
295
255
 
296
256
 
297
257
  ## Documentation Scoping
298
258
 
299
- Its possible to only generate Swagger documentation for a subset of your application controllers
300
- to do this you just need to use the `controllers_path` config option.
301
- For instance to only generate documentation for the controllers under `app/controllers/api` you
302
- need do this:
259
+ Limit documentation to a subset of controllers:
303
260
 
304
- # config/initializers/swaggard.rb
305
261
  Swaggard.configure do |config|
306
- ...
307
262
  config.controllers_path = "#{Rails.root}/app/controllers/api/**/*.rb"
308
- ...
309
263
  end
310
264
 
311
- The default value for `controllers_path` is `"#{Rails.root}/app/controllers/**/*.rb"`.
265
+ Default: `"#{Rails.root}/app/controllers/**/*.rb"`.
312
266
 
313
267
 
314
268
  ## More Information
315
269
 
316
- - [Swagger](http://swagger.io)
317
- - [Swagger-ui](https://github.com/wordnik/swagger-ui)
318
- - [Yard](https://github.com/lsegal/yard)
270
+ - [OpenAPI 3.1 Specification](https://spec.openapis.org/oas/v3.1.0)
271
+ - [Swagger UI](https://github.com/swagger-api/swagger-ui)
272
+ - [YARD](https://github.com/lsegal/yard)
319
273
 
320
274
 
321
275
  ## Author
@@ -325,5 +279,4 @@ The default value for `controllers_path` is `"#{Rails.root}/app/controllers/**/*
325
279
 
326
280
  ## Credits
327
281
 
328
- [Chris Trinh](https://github.com/chtrinh) author of [SwaggerYard](https://github.com/synctv/swagger_yard) in which this gem is
329
- inspired and used a base.
282
+ [Chris Trinh](https://github.com/chtrinh) author of [SwaggerYard](https://github.com/synctv/swagger_yard), which this gem is inspired by and based on.
@@ -2,61 +2,59 @@
2
2
  <html>
3
3
  <head>
4
4
  <title>Swagger UI</title>
5
-
6
5
  <%= favicon_link_tag 'swaggard/favicon-32x32.png', sizes: '32x32' %>
7
6
  <%= favicon_link_tag 'swaggard/favicon-16x16.png', sizes: '16x16' %>
8
-
9
- <%= stylesheet_link_tag 'swaggard/application', media: :screen %>
10
- <%= stylesheet_link_tag 'swaggard/application_print', media: :print %>
11
-
12
- <%= javascript_include_tag 'swaggard/application' %>
13
- <%= javascript_include_tag "swaggard/lang/#{Swaggard.configuration.language}" %>
14
-
15
- <%= javascript_tag do %>
16
- window.default_content_type = '<%= Swaggard.configuration.default_content_type %>';
17
- <% end %>
18
-
7
+ <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.32.0/swagger-ui.css">
19
8
  </head>
20
-
21
- <body class='swagger-section' data-doc-path='<%= doc_path(format: :json)%>'>
22
- <div id='header'>
23
- <div class='swagger-ui-wrap'>
24
- <a id='logo' href='http://swagger.io'>
25
- <%= image_tag 'swaggard/logo_small.png', class: 'logo__img', alt: 'swagger', height: 30, width: 30 %>
26
- <span class='logo__title'>swagger</span>
27
- </a>
28
- <%= form_tag '', id: :api_selector do %>
29
-
30
- <div>
31
- <div class='input'><input placeholder='http://example.com/api_docs/swagger.json' id='input_baseUrl' name='baseUrl' type='text'/></div>
32
-
33
- <div class='input'>
34
- <input class='additional_parameter' data-parameter-key='<%= @authentication_key %>'
35
- data-parameter-type='<%= @authentication_type %>'
36
- placeholder='<%= @authentication_key %>'
37
- type='text'
38
- value='<%= @authentication_value %>'/>
39
- </div>
40
-
41
- <div class='input'><a id='explore' class='header__btn' href='#' data-sw-translate>Explore</a></div>
42
- </div>
43
- <% Swaggard.configuration.additional_parameters.each do |additional_parameter|%>
44
- <div class='input'>
45
- <input class='additional_parameter' data-parameter-key='<%= additional_parameter[:key] %>'
46
- data-parameter-type='<%= additional_parameter[:type] %>'
47
- placeholder='<%= additional_parameter[:key] %>'
48
- type='text'
49
- value='<%= additional_parameter[:value] %>'/>
50
- </div>
51
- <% end %>
52
- <div id='auth_container'></div>
53
- <% end %>
54
- <br>
55
- <br>
56
- </div>
57
- </div>
58
-
59
- <div id='message-bar' class='swagger-ui-wrap' data-sw-translate>&nbsp;</div>
60
- <div id='swagger-ui-container' class='swagger-ui-wrap'></div>
9
+ <body>
10
+ <div id="swagger-ui"></div>
11
+
12
+ <script id="swagger-config" type="application/json">
13
+ <%= {
14
+ url: doc_path(format: :json),
15
+ authType: @authentication_type,
16
+ authKey: @authentication_key,
17
+ authValue: @authentication_value,
18
+ additionalParameters: Swaggard.configuration.additional_parameters
19
+ }.to_json.html_safe %>
20
+ </script>
21
+
22
+ <script src="https://unpkg.com/swagger-ui-dist@5.32.0/swagger-ui-bundle.js"></script>
23
+ <script>
24
+ window.onload = function() {
25
+ var config = JSON.parse(document.getElementById('swagger-config').textContent);
26
+
27
+ SwaggerUIBundle({
28
+ url: config.url,
29
+ dom_id: '#swagger-ui',
30
+ deepLinking: true,
31
+ presets: [SwaggerUIBundle.presets.apis],
32
+ plugins: [SwaggerUIBundle.plugins.DownloadUrl],
33
+ requestInterceptor: function(request) {
34
+ if (config.authValue) {
35
+ if (config.authType === 'header') {
36
+ request.headers[config.authKey] = config.authValue;
37
+ } else {
38
+ var url = new URL(request.url);
39
+ url.searchParams.set(config.authKey, config.authValue);
40
+ request.url = url.toString();
41
+ }
42
+ }
43
+
44
+ (config.additionalParameters || []).forEach(function(param) {
45
+ if (param.type === 'header') {
46
+ request.headers[param.key] = param.value;
47
+ } else if (param.type === 'query') {
48
+ var url = new URL(request.url);
49
+ url.searchParams.set(param.key, param.value);
50
+ request.url = url.toString();
51
+ }
52
+ });
53
+
54
+ return request;
55
+ }
56
+ });
57
+ }
58
+ </script>
61
59
  </body>
62
60
  </html>
@@ -34,25 +34,31 @@ module Swaggard
34
34
  license = { 'name' => Swaggard.configuration.license_name }
35
35
  license['url'] = Swaggard.configuration.license_url if Swaggard.configuration.license_url.present?
36
36
 
37
- {
38
- 'swagger' => Swaggard.configuration.swagger_version,
39
- 'info' => {
40
- 'version' => Swaggard.configuration.api_version,
41
- 'title' => Swaggard.configuration.title,
42
- 'description' => Swaggard.configuration.description,
43
- 'termsOfService' => Swaggard.configuration.tos,
44
- 'contact' => contact,
45
- 'license' => license,
46
- },
47
- 'host' => Swaggard.configuration.host,
48
- 'basePath' => Swaggard.configuration.api_base_path,
49
- 'schemes' => Swaggard.configuration.schemes,
50
- 'consumes' => Swaggard.configuration.api_formats.map { |format| "application/#{format}" },
51
- 'produces' => Swaggard.configuration.api_formats.map { |format| "application/#{format}" },
52
- 'tags' => @tags.map { |_, tag| tag.to_doc },
53
- 'paths' => Hash[@paths.values.map { |path| [format_path(path.path), path.to_doc] }],
54
- 'definitions' => Hash[@definitions.merge(Swaggard.configuration.definitions).map { |id, definition| [id, definition.to_doc(@definitions)] }]
55
- }.merge(security_definitions)
37
+ config = Swaggard.configuration
38
+ server_url = "#{config.schemes.first}://#{config.host}#{config.api_base_path}"
39
+
40
+ info = {
41
+ 'version' => config.api_version,
42
+ 'title' => config.title,
43
+ 'description' => config.description,
44
+ 'contact' => contact,
45
+ 'license' => license,
46
+ }
47
+ info['termsOfService'] = config.tos if config.tos.present?
48
+
49
+ doc = {
50
+ 'openapi' => '3.1.0',
51
+ 'info' => info,
52
+ 'servers' => [{ 'url' => server_url }],
53
+ 'tags' => @tags.map { |_, tag| tag.to_doc },
54
+ 'paths' => Hash[@paths.values.map { |path| [format_path(path.path), path.to_doc] }],
55
+ }
56
+
57
+ components = build_components
58
+ doc['components'] = components unless components.empty?
59
+ doc['security'] = config.security unless config.security.empty?
60
+
61
+ doc
56
62
  end
57
63
 
58
64
  private
@@ -63,21 +69,29 @@ module Swaggard
63
69
  path.gsub(Swaggard.configuration.api_base_path, '')
64
70
  end
65
71
 
66
- def security_definitions
67
- security_definitions = Swaggard.configuration.security_definitions
72
+ def build_components
73
+ custom_type_schemas = Swaggard.configuration.custom_types
74
+ .transform_keys { |k| Swaggard.ref_name(k) }
68
75
 
69
- return {} if security_definitions.empty? && Swaggard.configuration.security.empty?
76
+ definition_schemas = Hash[
77
+ @definitions.merge(Swaggard.configuration.definitions)
78
+ .map { |id, definition| [Swaggard.ref_name(id), definition.to_doc(@definitions)] }
79
+ ]
80
+
81
+ schemas = custom_type_schemas.merge(definition_schemas)
82
+
83
+ security_schemes = Swaggard.configuration.security_definitions
70
84
 
71
85
  Swaggard.configuration.security.flat_map(&:keys).each do |authentication_type|
72
- next if security_definitions.key?(authentication_type)
86
+ next if security_schemes.key?(authentication_type)
73
87
 
74
- warn "#{authentication_type} not supported by securityDefinitions"
88
+ warn "#{authentication_type} not supported by components/securitySchemes"
75
89
  end
76
90
 
77
- {
78
- 'securityDefinitions' => security_definitions,
79
- 'security' => Swaggard.configuration.security,
80
- }
91
+ result = {}
92
+ result['schemas'] = schemas unless schemas.empty?
93
+ result['securitySchemes'] = security_schemes unless security_schemes.empty?
94
+ result
81
95
  end
82
96
  end
83
97
  end
@@ -14,7 +14,7 @@ module Swaggard
14
14
 
15
15
  attr_accessor :controllers_path, :models_paths, :routes
16
16
 
17
- attr_writer :swagger_version, :api_base_path, :api_version, :api_formats, :title,
17
+ attr_writer :api_base_path, :api_version, :api_formats, :title,
18
18
  :description, :tos, :contact_email, :contact_name, :contact_url, :host,
19
19
  :authentication_type, :authentication_key, :authentication_value,
20
20
  :access_username, :access_password, :default_content_type, :use_cache,
@@ -23,10 +23,6 @@ module Swaggard
23
23
  :default_response_status_code, :excluded_paths, :path_parameter_description,
24
24
  :ignore_put_if_patch_exists, :ignore_untagged_controllers
25
25
 
26
- def swagger_version
27
- @swagger_version ||= '2.0'
28
- end
29
-
30
26
  def api_version
31
27
  @api_version ||= '0.1'
32
28
  end
@@ -9,15 +9,6 @@ module Swaggard
9
9
  initializer 'swaggard.finisher_hook', after: :finisher_hook do |app|
10
10
  app.reload_routes!
11
11
 
12
- if Rails.env.development? && !rake? && !app.methods.include?(:assets_manifest)
13
- warn <<~END
14
- [Swaggard] It seems you are using an api only rails setup, but swaggard
15
- [Swaggard] web app needs sprockets in order to work. Make sure to add
16
- [Swaggard] require 'sprockets/railtie'.
17
- [Swaggard] If you plan to use it
18
- END
19
- end
20
-
21
12
  Swaggard.configure do |config|
22
13
  unless config.controllers_path
23
14
  config.controllers_path = "#{app.root}/app/controllers/**/*.rb"
@@ -32,6 +23,5 @@ module Swaggard
32
23
 
33
24
  Swaggard.register_custom_yard_tags!
34
25
  end
35
-
36
26
  end
37
27
  end
@@ -41,7 +41,7 @@ module Swaggard
41
41
  end
42
42
 
43
43
  def route_path(route)
44
- path = route.path.spec.to_s
44
+ path = route.path.spec.to_s.dup
45
45
 
46
46
  path.gsub!('(.:format)', '')
47
47
  route.required_parts.each { |part| path.gsub!(":#{part}", "{#{part}}") }