swagger-doc-rails 1.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 (35) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Appraisals +7 -0
  4. data/CHANGELOG.md +120 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +448 -0
  8. data/Rakefile +10 -0
  9. data/certs/gem-public_cert.pem +21 -0
  10. data/lib/swagger/docs.rb +14 -0
  11. data/lib/swagger/docs/api_declaration_file.rb +125 -0
  12. data/lib/swagger/docs/api_declaration_file_metadata.rb +22 -0
  13. data/lib/swagger/docs/config.rb +63 -0
  14. data/lib/swagger/docs/dsl.rb +119 -0
  15. data/lib/swagger/docs/generator.rb +327 -0
  16. data/lib/swagger/docs/impotent_methods.rb +24 -0
  17. data/lib/swagger/docs/methods.rb +52 -0
  18. data/lib/swagger/docs/task.rb +9 -0
  19. data/lib/swagger/docs/version.rb +5 -0
  20. data/lib/tasks/swagger.rake +10 -0
  21. data/spec/fixtures/controllers/application_controller.rb +4 -0
  22. data/spec/fixtures/controllers/custom_resource_path_controller.rb +18 -0
  23. data/spec/fixtures/controllers/ignored_controller.rb +6 -0
  24. data/spec/fixtures/controllers/multiple_routes_controller.rb +13 -0
  25. data/spec/fixtures/controllers/nested_controller.rb +18 -0
  26. data/spec/fixtures/controllers/sample_controller.rb +81 -0
  27. data/spec/lib/swagger/docs/api_declaration_file_metadata_spec.rb +48 -0
  28. data/spec/lib/swagger/docs/api_declaration_file_spec.rb +203 -0
  29. data/spec/lib/swagger/docs/config_spec.rb +48 -0
  30. data/spec/lib/swagger/docs/dsl_spec.rb +17 -0
  31. data/spec/lib/swagger/docs/generator_spec.rb +451 -0
  32. data/spec/lib/swagger/docs/methods.rb +15 -0
  33. data/spec/spec_helper.rb +56 -0
  34. data/swagger-doc-rails.gemspec +31 -0
  35. metadata +175 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed7aba823f2b20ecc0ff55b80b3ac4599ca5c3a1
4
+ data.tar.gz: 06da8e3f9423c2bb114da219e76b58956e957a09
5
+ SHA512:
6
+ metadata.gz: aec7c12e5806c1bc9056b07d6c11841b12053ac73766ad41d8027bc962f11ae6ed6592db15900a3e57c3f9c362fea54880987961ef62c5266eba43562d90e721
7
+ data.tar.gz: f0ad066914f44470bea5176b300be91900498d6f454e48fbfb672a525f709a656ef101c8ff9b4fc163cfa4ac6664ab1190b4d9f76435e80b973ad2ceb558b72a
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ public
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "rails-3" do
2
+ gem "rails", "~> 3.2.16"
3
+ end
4
+
5
+ appraise "rails-4" do
6
+ gem "rails", "~> 4.0.0"
7
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,120 @@
1
+ ## 0.2.9
2
+
3
+ Fix gem dependencies to support Rails 5 #141
4
+
5
+ ## 0.2.8
6
+
7
+ Accept success status (#134) - thanks to @dcarneiro
8
+
9
+ ## 0.2.7
10
+
11
+ - Fix issue "NoMethodError: undefined method `<' for false:FalseClass" (#133) - thanks to @heaven
12
+
13
+ ## 0.2.6
14
+
15
+ - swagger_controller DSL can accept a resource_path which will be used over a generated path #126 @sb8244
16
+
17
+ ## 0.2.5
18
+
19
+ - Enabled option to set 'items' inside swagger_api method #99 @krakatoa
20
+
21
+ ## 0.2.4
22
+
23
+ - Parent controller option for register_apis config. #123 @mskubenich
24
+
25
+ ## 0.2.3
26
+
27
+ - Added property_list to SwaggerModelDSL #108 @dr-impossible
28
+
29
+ ## 0.2.2
30
+
31
+ - Support multiple route methods #128 @frodrigo
32
+
33
+ ## 0.2.1
34
+
35
+ - Add support for Authorizations (OAuth 2.0) - Thanks to @RKushnir #97
36
+
37
+ ## 0.2.0
38
+
39
+ - Additional logging for generation failures (suggested in #81)
40
+ - Added api_file_name to config #88
41
+ - Add support for multiple base api controllers. #93
42
+ - Change success status to ok #89
43
+ - Address issue with missing slashes - remove trailing slash from base paths and add slash before api paths #117
44
+
45
+ ## 0.1.9
46
+
47
+ - Adding support for multiple engines #65
48
+ - Add ability for swagger_api to accept parameters (e.g. consumes, produces)
49
+ - Update dependencies #64
50
+ - Address issue with routing verbs where some verbs do not have a route.verb.source attribute only route.verb #58
51
+ - Add ability to set custom attributes (like info block) on api-docs file #67
52
+ - Ensure API endpoint/nickname (e.g. "Api::V1::Some#update") is only written out once per resource file. Addresses PATCH, POST duplication issue #70
53
+ - Add "consumes" dsl method #74
54
+ - Expose API version on transform_path for easier “No Server Integrations” #79
55
+
56
+ ## 0.1.8
57
+
58
+ - Fix issue with gem build open-ended dependency warnings in gemspec
59
+ - Fix issue where param_list doesn't output parameter description #57
60
+
61
+ ## 0.1.7
62
+
63
+ - Make camelizing of model properties configurable. #55
64
+
65
+ ## 0.1.6
66
+
67
+ - Document notes DSL
68
+ - Get rid of unnecessary ternary operator in dsl.rb #54
69
+ - Fix development dependencies gems requirements #53
70
+ - Add support for the `notes` property #52
71
+ - Config's base_api_controller is configurable #51
72
+
73
+ ## 0.1.5
74
+ - Delay processing docs DSL to allow changing the context of the controllers #47 @ldnunes
75
+
76
+ ## 0.1.4
77
+ - An undocumentated action in a documented controller should not raise errors #43 @ldnunes
78
+ - Allow reopening of docs definition for the swagger_api DSL command #44 @ldnunes
79
+ - Refactor write_docs to split the documentation generation from file writing #45 @ldnunes
80
+
81
+ ## 0.1.3
82
+ - Fix issue where empty path throws error
83
+
84
+ ## 0.1.2
85
+ - Add suport for Swagger models
86
+ - Use ActionControlller::Base instead of ApplicationController. fixes #27
87
+ - Status codes for response
88
+ - Path generation fixes #26 @stevschmid
89
+ - Ignore path filtering when no params are set
90
+ - Add param_list helper for generating enums/lists
91
+ - Improve structure of generator class - break up large methods
92
+ - Fix the destination path of the resource files #30
93
+
94
+ ## 0.1.1
95
+ - Add support for Rails engines (@fotinakis)
96
+ - Filter out path parameters if the parameter is not in the path (@stevschmid)
97
+
98
+ ## 0.1.0
99
+
100
+ - Add CHANGELOG.md
101
+ - Add `api_extension_type` option (support for other route .formats)
102
+ - Rails Appraisals
103
+ - Add configuration options table to README documentation
104
+ - Guidance on inheritance and asset pre-compilation
105
+ - Custom response message error text can now be set
106
+ - Ability to override base controller with `base_api_controller` method
107
+ - Default configuration for Generator
108
+ - Fix typo in README.md
109
+
110
+ ##0.0.3
111
+
112
+ - Documentation
113
+
114
+ ## 0.0.2
115
+
116
+ - Add `base_path` option
117
+
118
+ ## 0.0.1
119
+
120
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # source 'https://rubygems.org'
2
+ source 'http://gems.ruby-china.com/'
3
+ # Specify your gem's dependencies in swagger-docs.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 zxhe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,448 @@
1
+ # Swagger::Docs::Rails
2
+ copied from [@swagger-docs](https://github.com/richhollis/swagger-docs)
3
+
4
+ Generates swagger-ui json files for rails apps with APIs. You add the swagger DSL to your controller classes and then run one rake task to generate the json files.
5
+
6
+ [gem]: https://rubygems.org/gems/swagger-docs
7
+
8
+ ## Swagger Version Specification Support
9
+
10
+ This project supports elements of the v2.0 swagger specification. only simple function v2.0 specification. not yet realize senior function, but is enough to generate api docs for rails project, and just need to declare paraneters then anything other is same with a rails project. I'm open to accepting a PR on this. Contact me if you are interested in helping with that effort - thanks!
11
+
12
+ ## Example usage
13
+
14
+ Here is an extract of the DSL from a user controller API class:
15
+
16
+ ```ruby
17
+ swagger_controller :users, "User Management"
18
+
19
+ swagger_api :index do
20
+ summary "Fetches all User items"
21
+ notes "This lists all the active users"
22
+ param :query, :page, :integer, :required, "Page number"
23
+ end
24
+ def index
25
+ # do anything like you do in rails
26
+ render json: {status: 200, msg: 'ok', data: {page_num: params[:page]}}
27
+ end
28
+
29
+ swagger_api :create do
30
+ summary "create a user"
31
+ param :query, :nickname, :string, :required, "nickname"
32
+ param_list :query, :gender, :string, :required, "gender", [ "male", "female" ]
33
+ param :query, :email, :string, :required, "email"
34
+ param :query, :phone, :string, :required, "phone"
35
+ response :unauthorized
36
+ response :not_acceptable
37
+ end
38
+ def create
39
+ # do anything like you do in rails
40
+ render json: {
41
+ status: 200,
42
+ msg: 'ok',
43
+ data: {
44
+ nickname: params[:nickname],
45
+ gender: params[:gender],
46
+ email: params[:email],
47
+ phone: params[:phone]
48
+ }
49
+ }
50
+ end
51
+ ```
52
+
53
+ ## Installation
54
+
55
+ Add this line to your application's Gemfile:
56
+
57
+ gem 'swagger-doc-rails'
58
+
59
+ And then execute:
60
+
61
+ $ bundle
62
+
63
+ Or install it yourself as:
64
+
65
+ $ gem install swagger-doc-rails
66
+
67
+ ## Usage
68
+
69
+ ### Create Initializer
70
+
71
+ Create an initializer in config/initializers (e.g. swagger_docs.rb) and define your APIs:
72
+
73
+ ```ruby
74
+ Swagger::Docs::Config.register_apis({
75
+ "1.0" => {
76
+ # the extension used for the API
77
+ :api_extension_type => :json,
78
+ # the output location where your .json files are written to
79
+ :api_file_path => "public",
80
+ # the URL base path to your API
81
+ :host => "localhost:3000",
82
+ # if you want to delete all .json files at each generation
83
+ :clean_directory => true,
84
+ # # Ability to setup base controller for each api version. Api::V1::SomeController for example.
85
+ :parent_controller => Api::ApplicationController,
86
+
87
+ :controller_base_path => 'api/',
88
+ # # add custom attributes to api-docs
89
+ :attributes => {
90
+ :info => {
91
+ "title" => "Swagger test",
92
+ "version" => '1.0.0',
93
+ "description" => "siple description.",
94
+ }
95
+ }
96
+ }
97
+ })
98
+ ```
99
+
100
+ #### Configuration options
101
+
102
+ The following table shows all the current configuration options and their defaults. The default will be used if you don't supply your own value.
103
+
104
+ <table>
105
+ <thead>
106
+ <tr>
107
+ <th>Option</th>
108
+ <th>Description</th>
109
+ <th>Default</th>
110
+ </tr>
111
+ </thead>
112
+ <tbody>
113
+
114
+ <tr>
115
+ <td><b>api_extension_type</b></td>
116
+ <td>The extension, if necessary, used for your API - e.g. :json or :xml </td>
117
+ <td>nil</td>
118
+ </tr>
119
+
120
+ <tr>
121
+ <td><b>api_file_path</b></td>
122
+ <td>The output file path where generated swagger-docs files are written to. </td>
123
+ <td>public/</td>
124
+ </tr>
125
+
126
+ <tr>
127
+ <td><b>base_path</b></td>
128
+ <td>The URI base path for your API - e.g. api.somedomain.com</td>
129
+ <td>/</td>
130
+ </tr>
131
+
132
+ <tr>
133
+ <td><b>base_api_controller / base_api_controllers</b></td>
134
+ <td>The base controller class your project uses; it or its subclasses will be where you call swagger_controller and swagger_api. An array of base controller classes may be provided.</td>
135
+ <td>ActionController::Base</td>
136
+ </tr>
137
+
138
+ <tr>
139
+ <td><b>clean_directory</b></td>
140
+ <td>When generating swagger-docs files this option specifies if the api_file_path should be cleaned first. This means that all files will be deleted in the output directory first before any files are generated.</td>
141
+ <td>false</td>
142
+ </tr>
143
+
144
+ <tr>
145
+ <td><b>formatting</b></td>
146
+ <td>Specifies which formatting method to apply to the JSON that is written. Available options: :none, :pretty</td>
147
+ <td>:pretty</td>
148
+ </tr>
149
+
150
+ <tr>
151
+ <td><b>camelize_model_properties</b></td>
152
+ <td>Camelizes property names of models. For example, a property name called first_name would be converted to firstName.</td>
153
+ <td>true</td>
154
+ </tr>
155
+
156
+ <tr>
157
+ <td><b>parent_controller</b></td>
158
+ <td>Assign a different controller to use for the configuration</td>
159
+ <td></td>
160
+ </tr>
161
+
162
+ </tbody>
163
+ </table>
164
+
165
+ #### Custom resource paths`(PR #126)
166
+
167
+ ```ruby
168
+ class Api::V1::UsersController < ApplicationController
169
+
170
+ swagger_controller :users, "User Management", resource_path: "/some/where"
171
+ ```
172
+
173
+ ### DRYing up common documentation
174
+
175
+ Suppose you have a header or a parameter that must be present on several controllers and methods. Instead of duplicating it on all the controllers you can do this on your API base controller:
176
+
177
+ ```ruby
178
+ class Api::BaseController < ActionController::Base
179
+ class << self
180
+ Swagger::Docs::Generator::set_real_methods
181
+
182
+ def inherited(subclass)
183
+ super
184
+ subclass.class_eval do
185
+ setup_basic_api_documentation
186
+ end
187
+ end
188
+
189
+ private
190
+ def setup_basic_api_documentation
191
+ [:index, :show, :create, :update, :delete].each do |api_action|
192
+ swagger_api api_action do
193
+ param :header, 'Authentication-Token', :string, :required, 'Authentication token'
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
199
+ ```
200
+
201
+ ### Run rake task to generate docs
202
+
203
+ ```
204
+ rake swagger:docs
205
+ ```
206
+
207
+ Swagger-ui JSON files should now be present in your api_file_path (e.g. ./public/api/)
208
+
209
+ #### Additional logging for generation failures
210
+
211
+ Errors aren't displayed by default. To see all error messages use the ```SD_LOG_LEVEL``` environment variable when running the rake task:
212
+
213
+ ```
214
+ SD_LOG_LEVEL=1 rake swagger:docs
215
+ ```
216
+
217
+ Currently only constantize errors are shown.
218
+
219
+ Errors are written to ```$stderr```. Error logging methods can be found in ```Config``` and can be overridden for custom behaviour.
220
+
221
+ ### Advanced Customization
222
+
223
+ #### Inheriting from a custom Api controller
224
+
225
+ By default swagger-docs is applied to controllers inheriting from ApplicationController.
226
+ If this is not the case for your application, use this snippet in your initializer
227
+ _before_ calling Swagger::Docs::Config#register_apis(...).
228
+
229
+ ```ruby
230
+ class Swagger::Docs::Config
231
+ def self.base_api_controller; Api::ApiController end
232
+ end
233
+ ```
234
+
235
+ #### Custom route discovery for supporting Rails Engines
236
+
237
+ By default, swagger-docs finds controllers by traversing routes in `Rails.application`.
238
+ To override this, you can customize the `base_application` config in an initializer:
239
+
240
+ ```ruby
241
+ class Swagger::Docs::Config
242
+ def self.base_application; Api::Engine end
243
+ end
244
+ ```
245
+
246
+ If you want swagger to find controllers in `Rails.application` and/or multiple
247
+ engines you can override `base_application` to return an array.
248
+
249
+ ```ruby
250
+ class Swagger::Docs::Config
251
+ def self.base_application; [Rails.application, Api::Engine, SomeOther::Engine] end
252
+ end
253
+ ```
254
+
255
+ Or, if you prefer you can override `base_applications` for this purpose. The plural
256
+ `base_applications` takes precedence over `base_application` and MUST return an
257
+ array.
258
+
259
+ ```ruby
260
+ class Swagger::Docs::Config
261
+ def self.base_applications; [Rails.application, Api::Engine, SomeOther::Engine] end
262
+ end
263
+ ```
264
+
265
+ #### Transforming the `path` variable
266
+
267
+ Swagger allows a distinction between the API documentation server and the hosted API
268
+ server through the `path` variable (see [Swagger: No server Integrations](https://github.com/wordnik/swagger-core/wiki/No-server-Integrations)). To override the default swagger-docs behavior, you can provide a `transform_path`
269
+ class method in your initializer:
270
+
271
+ ```ruby
272
+ class Swagger::Docs::Config
273
+ def self.transform_path(path, api_version)
274
+ "http://example.com/api-docs/#{api_version}/#{path}"
275
+ end
276
+ end
277
+ ```
278
+
279
+ The transformation will be applied to all API `path` values in the generated `api-docs.json` file.
280
+
281
+ #### Precompile
282
+
283
+ It is best-practice *not* to keep documentation in version control. An easy way
284
+ to integrate swagger-docs into a conventional deployment setup (e.g. capistrano,
285
+ chef, or opsworks) is to piggyback on the 'assets:precompile' rake task. And don't forget
286
+ to add your api documentation directory to .gitignore in this case.
287
+
288
+ ```ruby
289
+ #Rakefile or lib/task/precompile_overrides.rake
290
+ namespace :assets do
291
+ task :precompile do
292
+ Rake::Task['assets:precompile'].invoke
293
+ Rake::Task['swagger:docs'].invoke
294
+ end
295
+ end
296
+ ```
297
+
298
+ ### Output files
299
+
300
+ api-docs.json output:
301
+
302
+ ```json
303
+ {
304
+ "swagger": "2.0",
305
+ "host": "localhost:3000",
306
+ "basePath": "/api/",
307
+ "schemes": [
308
+ "http"
309
+ ],
310
+ "paths": {
311
+ "/users": {
312
+ "get": {
313
+ "summary": "Fetches all User items",
314
+ "description": "User Management",
315
+ "parameters": [
316
+ {
317
+ "name": "page",
318
+ "in": "query",
319
+ "required": true,
320
+ "description": "Page number",
321
+ "type": "integer"
322
+ },
323
+ {
324
+ "name": "nested_id",
325
+ "in": "query",
326
+ "required": true,
327
+ "description": "Team Id",
328
+ "type": "integer"
329
+ }
330
+ ],
331
+ "responses": {
332
+ "200": {
333
+ "description": "User Management",
334
+ "schema": {
335
+ }
336
+ }
337
+ }
338
+ }
339
+ },
340
+ "/users/": {
341
+ "post": {
342
+ "summary": "创建一个新用户",
343
+ "description": "User Management",
344
+ "parameters": [
345
+ {
346
+ "name": "nickname",
347
+ "in": "query",
348
+ "required": true,
349
+ "description": "昵称",
350
+ "type": "string"
351
+ },
352
+ {
353
+ "name": "gender",
354
+ "in": "query",
355
+ "required": true,
356
+ "description": "性别",
357
+ "type": "string"
358
+ },
359
+ {
360
+ "name": "email",
361
+ "in": "query",
362
+ "required": true,
363
+ "description": "邮箱",
364
+ "type": "string"
365
+ },
366
+ {
367
+ "name": "phone",
368
+ "in": "query",
369
+ "required": true,
370
+ "description": "手机",
371
+ "type": "string"
372
+ }
373
+ ],
374
+ "responses": {
375
+ "200": {
376
+ "description": "User Management",
377
+ "schema": {
378
+ "type": "pbject",
379
+ "items": {
380
+ "properties": {
381
+ "nickname": {
382
+ "type": "string"
383
+ },
384
+ "gender": {
385
+ "type": "string"
386
+ },
387
+ "email": {
388
+ "type": "string"
389
+ },
390
+ "phone": {
391
+ "type": "string"
392
+ }
393
+ }
394
+ }
395
+ }
396
+ }
397
+ }
398
+ }
399
+ },
400
+ "/users/{id}": {
401
+ "get": {
402
+ "summary": "Fetches a single User item",
403
+ "description": "User Management",
404
+ "parameters": [
405
+ {
406
+ "name": "id",
407
+ "in": "path",
408
+ "required": false,
409
+ "description": "User Id",
410
+ "type": "integer"
411
+ }
412
+ ],
413
+ "responses": {
414
+ "200": {
415
+ "description": "User Management",
416
+ "schema": {
417
+ }
418
+ }
419
+ }
420
+ }
421
+ },
422
+ },
423
+ "info": {
424
+ "title": "Swagger test",
425
+ "version": "1.0.0",
426
+ "description": "simple description."
427
+ }
428
+ }
429
+
430
+ ```
431
+
432
+ ## Thanks to our contributors
433
+
434
+ Welcome contributors for making swagger-doc-railss even better.
435
+
436
+ ## Related Projects
437
+
438
+ **[@fotinakis](https://github.com/richhollis/swagger-docs)**
439
+
440
+ ## Contributing
441
+
442
+ When raising a Pull Request please ensure that you have provided good test coverage for the request you are making.
443
+
444
+ 1. Fork it
445
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
446
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
447
+ 4. Push to the branch (`git push origin my-new-feature`)
448
+ 5. Create new Pull Request