zero-rails_openapi 1.3.3 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +91 -0
- data/Gemfile.lock +7 -5
- data/LICENSE.txt +1 -1
- data/README.md +182 -55
- data/documentation/examples/auto_gen_desc.rb +9 -11
- data/documentation/examples/auto_gen_doc.rb +3 -3
- data/documentation/examples/examples_controller.rb +27 -29
- data/documentation/examples/goods_doc.rb +9 -8
- data/documentation/examples/open_api.rb +28 -17
- data/documentation/examples/output_example.json +1417 -798
- data/lib/oas_objs/combined_schema.rb +28 -0
- data/lib/oas_objs/example_obj.rb +1 -1
- data/lib/oas_objs/helpers.rb +3 -2
- data/lib/oas_objs/media_type_obj.rb +1 -1
- data/lib/oas_objs/param_obj.rb +6 -5
- data/lib/oas_objs/response_obj.rb +1 -1
- data/lib/oas_objs/schema_obj.rb +11 -8
- data/lib/open_api.rb +8 -0
- data/lib/open_api/config.rb +9 -52
- data/lib/open_api/config_dsl.rb +27 -9
- data/lib/open_api/dsl.rb +17 -22
- data/lib/open_api/dsl/api_info_obj.rb +24 -21
- data/lib/open_api/dsl/common_dsl.rb +1 -0
- data/lib/open_api/dsl/ctrl_info_obj.rb +35 -7
- data/lib/open_api/dsl/helpers.rb +8 -9
- data/lib/open_api/generator.rb +35 -27
- data/lib/open_api/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc28a19288b67c33764363217945eabe353cbe19
|
4
|
+
data.tar.gz: 383973acb33b8dbbf4e0706e3854aadd252709a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c599c44d70844213307c8c00e3aac227776df6446236fde17cb021468187ac286bebfae40a82bc316ce553511972b74d72c838f0453548762628b5ed676e4581
|
7
|
+
data.tar.gz: 9637974d052bfa81702bd379034454a5a85fa20b2a86f2f74f0f117585f03268a5b7f52563f9ddb69e2657519ade1ba9e20c4c8595efb4a5fb0b4d24d7283e18
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# This is the configuration used to check the rubocop source code.
|
2
|
+
|
3
|
+
#require: rbocop/cop/internal_affairs
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
# Include common Ruby source files.
|
7
|
+
Exclude:
|
8
|
+
- 'spec/**/*'
|
9
|
+
- 'zero-params_processor.gemspec'
|
10
|
+
- 'Gemfile'
|
11
|
+
- 'bin/**/*'
|
12
|
+
DisplayCopNames: true
|
13
|
+
TargetRubyVersion: 2.4
|
14
|
+
TargetRailsVersion: 5.1
|
15
|
+
|
16
|
+
Style/FrozenStringLiteralComment:
|
17
|
+
Enabled: false
|
18
|
+
|
19
|
+
Metrics/LineLength:
|
20
|
+
Max: 1200
|
21
|
+
|
22
|
+
Metrics/ClassLength:
|
23
|
+
Max: 1200
|
24
|
+
|
25
|
+
Metrics/MethodLength:
|
26
|
+
Max: 1200
|
27
|
+
|
28
|
+
Metrics/ModuleLength:
|
29
|
+
Max: 1200
|
30
|
+
|
31
|
+
Metrics/BlockLength:
|
32
|
+
Max: 1200
|
33
|
+
|
34
|
+
Style/Documentation:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Layout/SpaceInsideBrackets:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Layout/SpaceInsideHashLiteralBraces:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Layout/SpaceInsidePercentLiteralDelimiters:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Style/AsciiComments:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Layout/IndentHash:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Layout/AlignHash:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Style/HashSyntax:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Style/ClassAndModuleChildren:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
Layout/EmptyLines:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Lint/UnneededSplatExpansion:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
#Style/StringLiterals:
|
68
|
+
# Enabled: true
|
69
|
+
# EnforcedStyle: double_quotes
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
Style/UnneededPercentQ:
|
75
|
+
Enabled: false
|
76
|
+
|
77
|
+
Style/RedundantSelf:
|
78
|
+
Enabled: false
|
79
|
+
|
80
|
+
Metrics/CyclomaticComplexity:
|
81
|
+
Enabled: false
|
82
|
+
|
83
|
+
Metrics/AbcSize:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
Metrics/PerceivedComplexity:
|
87
|
+
Enabled: false
|
88
|
+
|
89
|
+
# False positives
|
90
|
+
Layout/EmptyLineBetweenDefs:
|
91
|
+
Enabled: false
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
zero-rails_openapi (1.
|
4
|
+
zero-rails_openapi (1.4.0)
|
5
5
|
activesupport (>= 3)
|
6
6
|
rails (>= 3)
|
7
7
|
|
@@ -53,15 +53,17 @@ GEM
|
|
53
53
|
erubi (1.7.0)
|
54
54
|
globalid (0.4.1)
|
55
55
|
activesupport (>= 4.2.0)
|
56
|
-
i18n (0.9.
|
56
|
+
i18n (0.9.0)
|
57
57
|
concurrent-ruby (~> 1.0)
|
58
58
|
loofah (2.1.1)
|
59
59
|
crass (~> 1.0.2)
|
60
60
|
nokogiri (>= 1.5.9)
|
61
|
-
mail (2.
|
62
|
-
|
61
|
+
mail (2.6.6)
|
62
|
+
mime-types (>= 1.16, < 4)
|
63
63
|
method_source (0.9.0)
|
64
|
-
|
64
|
+
mime-types (3.1)
|
65
|
+
mime-types-data (~> 3.2015)
|
66
|
+
mime-types-data (3.2016.0521)
|
65
67
|
mini_portile2 (2.3.0)
|
66
68
|
minitest (5.10.3)
|
67
69
|
nio4r (2.1.0)
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -8,12 +8,9 @@
|
|
8
8
|
|
9
9
|
## Contributing
|
10
10
|
|
11
|
-
**Hi, here is ZhanDao
|
12
|
-
I
|
13
|
-
I'm looking forward to your
|
14
|
-
|
15
|
-
Currently, I think the most important TODO is the Unit Test (RSpec, I want is),
|
16
|
-
but I dont have enough time now = ▽ =
|
11
|
+
**Hi, here is ZhanDao = ▽ =
|
12
|
+
I think it's a very useful tool when you want to write API document clearly.
|
13
|
+
I'm looking forward to your issue and PR, thanks!**
|
17
14
|
|
18
15
|
## Table of Contents
|
19
16
|
|
@@ -21,6 +18,8 @@
|
|
21
18
|
- [Installation](#installation)
|
22
19
|
- [Configure](#configure)
|
23
20
|
- [Usage - DSL](#usage---dsl)
|
21
|
+
- [DSL methods inside `open_api` and `api_dry`'s block](#dsl-methods-inside-open_api-and-api_drys-block)
|
22
|
+
- [DSL methods inside `components`'s block](#dsl-methods-inside-componentss-block-code-source-ctrlinfoobj-)
|
24
23
|
- [Usage - Generate JSON documentation file](#usage---generate-json-documentation-file)
|
25
24
|
- [Usage - Use Swagger UI(very beautiful web page) to show your Documentation](#usage---use-swagger-uivery-beautiful-web-page-to-show-your-documentation)
|
26
25
|
- [Tricks](#tricks)
|
@@ -28,8 +27,10 @@
|
|
28
27
|
- [Global DRYing](#trick2---global-drying)
|
29
28
|
- [Auto generate description](#trick3---auto-generate-description)
|
30
29
|
- [Skip or Use parameters define in api_dry](#trick4---skip-or-use-parameters-define-in-api_dry)
|
31
|
-
- [Atuo Generate index/show Actions's Responses Based on DB Schema](#trick5---auto-generate-indexshow-actionss-
|
30
|
+
- [Atuo Generate index/show Actions's Responses Based on DB Schema](#trick5---auto-generate-indexshow-actionss-response-types-based-on-db-schema)
|
31
|
+
- [Combined Schema (oneOf / allOf / anyOf / not)]()
|
32
32
|
- [Troubleshooting](#troubleshooting)
|
33
|
+
- [About `OpenApi.docs` and `OpenApi.paths_index`]()
|
33
34
|
|
34
35
|
## About OAS
|
35
36
|
|
@@ -73,7 +74,7 @@
|
|
73
74
|
# [REQUIRED] The output location where .json doc file will be written to.
|
74
75
|
c.file_output_path = 'public/open_api'
|
75
76
|
|
76
|
-
c.
|
77
|
+
c.open_api_docs = {
|
77
78
|
homepage_api: {
|
78
79
|
# [REQUIRED] ZRO will scan all the descendants of root_controller, then generate their docs.
|
79
80
|
root_controller: Api::V1::BaseController,
|
@@ -105,14 +106,15 @@
|
|
105
106
|
|
106
107
|
OpenApi::Config.tap do |c|
|
107
108
|
c.instance_eval do
|
108
|
-
|
109
|
+
open_api :homepage_api, root_controller: ApiDoc
|
109
110
|
info version: '1.0.0', title: 'Homepage APIs'
|
110
111
|
end
|
111
112
|
end
|
112
113
|
```
|
113
114
|
|
114
|
-
For more detailed configuration: [open_api.rb](
|
115
|
-
See all the settings you can configure: [config.rb](
|
115
|
+
For more detailed configuration: [open_api.rb](documentation/examples/open_api.rb)
|
116
|
+
See all the settings you can configure: [config.rb](lib/open_api/config.rb)
|
117
|
+
See all the Config DSL: [config_dsl.rb](lib/open_api/config_dsl.rb)
|
116
118
|
|
117
119
|
## Usage - DSL
|
118
120
|
|
@@ -131,17 +133,17 @@
|
|
131
133
|
|
132
134
|
```ruby
|
133
135
|
class Api::V1::ExamplesController < Api::V1::BaseController
|
134
|
-
|
136
|
+
api :index, 'GET list' do
|
135
137
|
query :page, Integer#, desc: 'page, greater than 1', range: { ge: 1 }, dft: 1
|
136
138
|
query :rows, Integer#, desc: 'per page', range: { ge: 1 }, default: 10
|
137
139
|
end
|
138
140
|
end
|
139
141
|
```
|
140
142
|
|
141
|
-
For more example, see [goods_doc.rb](
|
142
|
-
[examples_controller.rb](
|
143
|
+
For more example, see [goods_doc.rb](documentation/examples/goods_doc.rb), and
|
144
|
+
[examples_controller.rb](documentation/examples/examples_controller.rb)
|
143
145
|
|
144
|
-
### DSL methods of controller ([source code](
|
146
|
+
### DSL methods of controller ([source code](lib/open_api/dsl.rb))
|
145
147
|
|
146
148
|
#### (1) `ctrl_path` (controller path) [optional]
|
147
149
|
|
@@ -182,7 +184,7 @@
|
|
182
184
|
end
|
183
185
|
|
184
186
|
# to use component
|
185
|
-
|
187
|
+
api :action, 'summary' do
|
186
188
|
query :doge, :DogSchema # to use a Schema component
|
187
189
|
param_ref :UidQuery # to use a Parameter component
|
188
190
|
response_ref :BadRqResp # to use a Response component
|
@@ -210,29 +212,29 @@
|
|
210
212
|
|
211
213
|
As you think, the block will be executed to each specified API(action) **firstly**.
|
212
214
|
|
213
|
-
#### (5) `
|
215
|
+
#### (5) `api` [required]
|
214
216
|
|
215
217
|
Define the specified API (controller action, in the following example is index).
|
216
218
|
|
217
219
|
```ruby
|
218
220
|
# method signature
|
219
|
-
|
221
|
+
api(action, summary = '', builder: nil, skip: [ ], use: [ ], &block)
|
220
222
|
# usage
|
221
|
-
|
223
|
+
api :index, '(SUMMARY) this api blah blah ...', builder: :index # block ...
|
222
224
|
```
|
223
225
|
If you pass `builder`, and `generate_jbuilder_file` is set to `true` (in your initializer),
|
224
226
|
ZRO will generate JBuilder file by using specified template called `index`.
|
225
|
-
About template settings, see: [open_api.rb](
|
227
|
+
About template settings, see: [open_api.rb](documentation/examples/open_api.rb)
|
226
228
|
|
227
229
|
`use` and `skip` options: to use or skip the parameters defined in `api_dry`.
|
228
230
|
|
229
231
|
```ruby
|
230
|
-
|
232
|
+
api :show, 'summary', use: [:id] # => it will only take :id from DRYed result.
|
231
233
|
```
|
232
234
|
|
233
|
-
### DSL methods inside [
|
235
|
+
### DSL methods inside [api]() and [api_dry]()'s block
|
234
236
|
|
235
|
-
[source code](
|
237
|
+
[source code](lib/open_api/dsl/api_info_obj.rb)
|
236
238
|
|
237
239
|
These following methods in the block describe the specified API action: description, valid?,
|
238
240
|
parameters, request body, responses, securities, servers.
|
@@ -304,15 +306,26 @@
|
|
304
306
|
param_ref :IdPath, :NameQuery, :TokenHeader
|
305
307
|
|
306
308
|
|
307
|
-
|
308
|
-
header(param_name, schema_type,
|
309
|
-
header!(param_name, schema_type,
|
310
|
-
query!(param_name, schema_type,
|
311
|
-
#
|
309
|
+
### method signature
|
310
|
+
header(param_name, schema_type = nil, one_of: nil, all_of: nil, any_of: nil, not: nil, **schema_hash)
|
311
|
+
header!(param_name, schema_type = nil, one_of: nil, all_of: nil, any_of: nil, not: nil, **schema_hash)
|
312
|
+
query!(param_name, schema_type = nil, one_of: nil, all_of: nil, any_of: nil, not: nil, **schema_hash)
|
313
|
+
# ...
|
314
|
+
### usage
|
312
315
|
header! 'Token', String
|
313
316
|
query! :readed, Boolean, must_be: true, default: false
|
314
317
|
# The same effect as above, but not simple
|
315
|
-
param
|
318
|
+
param :query, :readed, Boolean, :req, must_be: true, default: false
|
319
|
+
#
|
320
|
+
# When schema_type is a Object
|
321
|
+
# (describe by hash, key means prop's name, value means prop's schema_type)
|
322
|
+
query :good, { name: String, price: Float, spec: { size: String, weight: Integer } }, desc: 'good info'
|
323
|
+
# Or you can use `type:` to sign the schema_type, maybe this is clearer for describing object
|
324
|
+
query :good, type: { name: String, price: Float, spec: { size: String, weight: Integer } }, desc: 'good info'
|
325
|
+
#
|
326
|
+
query :good_name, type: String # It's also OK, but some superfluous
|
327
|
+
query :good_name, String # recommended
|
328
|
+
# About Combined Schema (`one_of` ..), see the link below.
|
316
329
|
|
317
330
|
|
318
331
|
# method signature
|
@@ -340,8 +353,10 @@
|
|
340
353
|
:wrong_input => [ -1, '' ]
|
341
354
|
}
|
342
355
|
```
|
356
|
+
|
357
|
+
[This trick show you how to define combined schema (by using `one_of` ..)]()
|
343
358
|
|
344
|
-
[**>> More About `param` DSL <<**](
|
359
|
+
[**>> More About `param` DSL <<**](documentation/parameter.md)
|
345
360
|
|
346
361
|
#### (4) `request_body` family methods (OAS - [Request Body Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#requestBodyObject))
|
347
362
|
|
@@ -408,7 +423,7 @@
|
|
408
423
|
1. **Notice:** Each API should only declare a request body
|
409
424
|
That is, all of the above methods you can only choose one of them.
|
410
425
|
(But **multiple media types** will be supported in the future).
|
411
|
-
2. `media_type`: we provide some [mapping](
|
426
|
+
2. `media_type`: we provide some [mapping](lib/oas_objs/media_type_obj.rb) from symbols to real media-types.
|
412
427
|
3. `schema_hash`: as above (see param).
|
413
428
|
**One thing that should be noted is: when use Hash writing, `scham_type` is writed in schema_hash using key :type.**
|
414
429
|
4. `exp_by` and `examples`: for the above example, the following has the same effect:
|
@@ -427,6 +442,7 @@
|
|
427
442
|
response_ref
|
428
443
|
default_response or dft_resp
|
429
444
|
error_response, other_response, oth_resp, error, err_resp # response's aliases, should be used in the error response context.
|
445
|
+
merge_to_resp
|
430
446
|
```
|
431
447
|
|
432
448
|
```ruby
|
@@ -439,15 +455,87 @@
|
|
439
455
|
response_ref(code_compkey_hash)
|
440
456
|
# usage
|
441
457
|
response_ref 700 => :AResp, 800 => :BResp
|
458
|
+
|
459
|
+
# method signature
|
460
|
+
merge_to_resp(code, by:)
|
461
|
+
# usage
|
462
|
+
merge_to_resp 200, by: {
|
463
|
+
data: {
|
464
|
+
type: String
|
465
|
+
}
|
466
|
+
}
|
467
|
+
```
|
468
|
+
|
469
|
+
**practice:** Combined with wrong class, automatically generate error responses. [AutoGenDoc](documentation/examples/auto_gen_doc.rb#L63)
|
470
|
+
|
471
|
+
#### (6) Authentication and Authorization
|
472
|
+
|
473
|
+
First of all, please make sure that you have read one of the following documents:
|
474
|
+
[OpenApi Auth](https://swagger.io/docs/specification/authentication/)
|
475
|
+
or [securitySchemeObject](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject)
|
476
|
+
|
477
|
+
##### Define Security Scheme
|
478
|
+
|
479
|
+
Use these DSL in your initializer or `components` block:
|
442
480
|
```
|
481
|
+
security_scheme # alias `auth_scheme`
|
482
|
+
base_auth # will call `security_scheme`
|
483
|
+
bearer_auth # will call `security_scheme`
|
484
|
+
api_key # will call `security_scheme`
|
485
|
+
```
|
486
|
+
It's very simple to use (if you understand the above document)
|
487
|
+
```ruby
|
488
|
+
# method signature
|
489
|
+
security_scheme(scheme_name, other_info)
|
490
|
+
# usage
|
491
|
+
security_scheme :BasicAuth, { type: 'http', scheme: 'basic', desc: 'basic auth' }
|
443
492
|
|
444
|
-
|
493
|
+
# method signature
|
494
|
+
base_auth(scheme_name, other_info = { })
|
495
|
+
bearer_auth(scheme_name, format = 'JWT', other_info = { })
|
496
|
+
api_key(scheme_name, field:, in:, **other_info)
|
497
|
+
# usage
|
498
|
+
base_auth :BasicAuth, desc: 'basic auth' # the same effect as ↑↑↑
|
499
|
+
bearer_auth :Token
|
500
|
+
api_key :ApiKeyAuth, field: 'X-API-Key', in: 'header', desc: 'pass api key to header'
|
501
|
+
```
|
502
|
+
|
503
|
+
##### Apply Security
|
445
504
|
|
446
|
-
|
505
|
+
```
|
506
|
+
# In initializer
|
507
|
+
# Global effectiveness
|
508
|
+
global_security_require
|
509
|
+
global_security # alias
|
510
|
+
global_auth # alias
|
511
|
+
|
512
|
+
# In `api`'s block
|
513
|
+
# Only valid for the current controller
|
514
|
+
security_require
|
515
|
+
security # alias
|
516
|
+
auth # alias
|
517
|
+
need_auth # alias
|
518
|
+
```
|
519
|
+
Name is different, signature and usage is similar.
|
520
|
+
```ruby
|
521
|
+
# method signature
|
522
|
+
security_require(scheme_name, scopes: [ ])
|
523
|
+
# usage
|
524
|
+
global_auth :Token
|
525
|
+
need_auth :Token
|
526
|
+
auth :OAuth, scopes: %w[ read_example admin ]
|
527
|
+
```
|
447
528
|
|
448
|
-
#### (7) `server
|
529
|
+
#### (7) Overriding Global Servers by `server`
|
449
530
|
|
450
|
-
|
531
|
+
```ruby
|
532
|
+
# method signature
|
533
|
+
server(url, desc)
|
534
|
+
# usage
|
535
|
+
server 'http://localhost:3000', 'local'
|
536
|
+
```
|
537
|
+
|
538
|
+
### DSL methods inside [components]()'s block ([code source](lib/open_api/dsl/ctrl_info_obj.rb):: CtrlInfoObj )
|
451
539
|
|
452
540
|
(Here corresponds to OAS [Components Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#componentsObject))
|
453
541
|
|
@@ -469,18 +557,20 @@
|
|
469
557
|
# method signature
|
470
558
|
schema(component_key, type, schema_hash)
|
471
559
|
# usage
|
472
|
-
schema :Dog => [ String, desc: 'dogee' ]
|
560
|
+
schema :Dog => [ String, desc: 'dogee' ] # <= schema_type is `String`
|
473
561
|
# advance usage
|
474
|
-
schema :Dog => [
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
562
|
+
schema :Dog => [
|
563
|
+
{
|
564
|
+
id!: Integer,
|
565
|
+
name: { type: String, must_be: 'name', desc: 'name' }
|
566
|
+
}, # <= this hash is schema type[1]
|
567
|
+
dft: { id: 1, name: 'pet' },
|
568
|
+
desc: 'dogee'
|
569
|
+
]
|
480
570
|
# or (unrecommended)
|
481
571
|
schema :Dog, { id!: Integer, name: String }, dft: { id: 1, name: 'pet' }, desc: 'dogee'
|
482
572
|
```
|
483
|
-
[1] see: [Type](
|
573
|
+
[1] see: [Type](documentation/parameter.md#type-schema_type)
|
484
574
|
|
485
575
|
## Usage - Generate JSON Documentation File
|
486
576
|
|
@@ -491,7 +581,7 @@
|
|
491
581
|
OpenApi.write_docs generate_files: !Rails.env.production?
|
492
582
|
|
493
583
|
# or run directly in console
|
494
|
-
OpenApi.write_docs
|
584
|
+
OpenApi.write_docs # will generate json doc files
|
495
585
|
```
|
496
586
|
|
497
587
|
Then the JSON files will be written to the directories you set. (Each API a file.)
|
@@ -500,8 +590,7 @@
|
|
500
590
|
|
501
591
|
Download [Swagger UI](https://github.com/swagger-api/swagger-ui) (version >= 2.3.0 support the OAS3)
|
502
592
|
to your project,
|
503
|
-
|
504
|
-
(window.onload >> SwaggerUIBundle >> url).
|
593
|
+
change the default JSON file path(url) in index.html.
|
505
594
|
In order to use it, you may have to enable CORS, [see](https://github.com/swagger-api/swagger-ui#cors-support)
|
506
595
|
|
507
596
|
## Tricks
|
@@ -528,7 +617,7 @@
|
|
528
617
|
class V1::ExamplesDoc < ApiDoc
|
529
618
|
ctrl_path 'api/v1/examples'
|
530
619
|
|
531
|
-
|
620
|
+
api :index do
|
532
621
|
# ...
|
533
622
|
end
|
534
623
|
end
|
@@ -540,7 +629,7 @@
|
|
540
629
|
|
541
630
|
Method `api_dry` is for DRY but its scope is limited to the current controller.
|
542
631
|
|
543
|
-
I have no idea of best practices, But you can look at this [file](
|
632
|
+
I have no idea of best practices, But you can look at this [file](documentation/examples/auto_gen_doc.rb).
|
544
633
|
The implementation of the file is: do `api_dry` when inherits the base controller inside `inherited` method.
|
545
634
|
|
546
635
|
You can use `sort` to specify the order of parameters.
|
@@ -572,30 +661,68 @@
|
|
572
661
|
'only offline': :offline,
|
573
662
|
'expensive goods': :get,
|
574
663
|
'cheap goods': :borrow,
|
575
|
-
|
664
|
+
}
|
576
665
|
```
|
577
|
-
Read this [file](
|
666
|
+
Read this [file](documentation/examples/auto_gen_desc.rb) to learn more.
|
578
667
|
|
579
668
|
### Trick4 - Skip or Use parameters define in api_dry
|
580
669
|
|
581
|
-
Pass `skip: []` and `use: []` to `
|
670
|
+
Pass `skip: []` and `use: []` to `api` like following code:
|
582
671
|
```ruby
|
583
|
-
|
672
|
+
api :index, 'desc', builder: :index, skip: [ :Token ]
|
584
673
|
```
|
585
674
|
|
586
|
-
Look at this [file](
|
675
|
+
Look at this [file](documentation/examples/goods_doc.rb) to learn more.
|
587
676
|
|
588
677
|
### Trick5 - Auto Generate index/show Actions's Response-Types Based on DB Schema
|
589
678
|
|
590
679
|
Use method `load_schema` in `api_dry`.
|
591
680
|
|
592
|
-
See this [file](
|
681
|
+
See this [file](documentation/examples/auto_gen_doc.rb#L51) for uasge information.
|
682
|
+
|
683
|
+
### Trick6 - Combined Schema (oneOf / allOf / anyOf / not)
|
593
684
|
|
685
|
+
```ruby
|
686
|
+
query :combination, one_of: [ :GoodSchema, String, { type: Integer, desc: 'integer input'}]
|
687
|
+
|
688
|
+
form '', data: {
|
689
|
+
:combination_in_form => { any_of: [ Integer, String ] }
|
690
|
+
}
|
691
|
+
|
692
|
+
schema :PetSchema => [ not: [ Integer, Boolean ] ]
|
693
|
+
```
|
694
|
+
|
695
|
+
OAS: [link1](https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/),
|
696
|
+
[link2](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#schemaObject)
|
594
697
|
|
595
698
|
## Troubleshooting
|
596
699
|
|
597
700
|
- **You wrote document of the current API, but not find in the generated json file?**
|
598
701
|
Check your routing settings.
|
702
|
+
- **Undefine method `match?`**
|
703
|
+
Monkey patches for `String` and `Symbol`:
|
704
|
+
```ruby
|
705
|
+
class String # Symbol
|
706
|
+
def match?(pattern); !match(pattern).nil? end
|
707
|
+
end
|
708
|
+
```
|
709
|
+
- **Report error when require `routes.rb`?***
|
710
|
+
1. Run `rails routes`.
|
711
|
+
2. Copy the output to a file, for example `config/routes.txt`.
|
712
|
+
ignore the file `config/routes.txt`.
|
713
|
+
3. Put `c.rails_routes_file = 'config/routes.txt'` to your ZRO config.
|
714
|
+
|
715
|
+
|
716
|
+
## About `OpenApi.docs` and `OpenApi.paths_index`
|
717
|
+
|
718
|
+
After `OpenApi.write_docs`, the above two module variables will be generated.
|
719
|
+
|
720
|
+
`OpenApi.docs`: A Hash with API names as keys, and documents of each APIs as values.
|
721
|
+
documents are instances of ActiveSupport::HashWithIndifferentAccess.
|
722
|
+
|
723
|
+
`OpenApi.paths_index`: Inverted index of controller path to API name mappings.
|
724
|
+
Like: `{ 'api/v1/examples' => :homepage_api }`
|
725
|
+
It's useful when you want to look up a document based on a controller and do something.
|
599
726
|
|
600
727
|
## Development
|
601
728
|
|
@@ -609,4 +736,4 @@
|
|
609
736
|
|
610
737
|
## Code of Conduct
|
611
738
|
|
612
|
-
Everyone interacting in the Zero-OpenApi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](
|
739
|
+
Everyone interacting in the Zero-OpenApi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|