zero-rails_openapi 1.3.2 → 1.3.3
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.
- checksums.yaml +4 -4
- data/Gemfile +0 -2
- data/Gemfile.lock +90 -3
- data/README.md +409 -357
- data/documentation/examples/examples_controller.rb +2 -2
- data/documentation/examples/goods_doc.rb +14 -12
- data/documentation/examples/open_api.rb +14 -4
- data/documentation/examples/{example_output_doc.json → output_example.json} +0 -0
- data/documentation/parameter.md +17 -12
- data/lib/oas_objs/schema_obj.rb +1 -1
- data/lib/open_api/config.rb +18 -18
- data/lib/open_api/config_dsl.rb +21 -2
- data/lib/open_api/dsl.rb +12 -13
- data/lib/open_api/dsl/api_info_obj.rb +27 -2
- data/lib/open_api/dsl/common_dsl.rb +1 -0
- data/lib/open_api/dsl/ctrl_info_obj.rb +1 -1
- data/lib/open_api/dsl/helpers.rb +4 -4
- data/lib/open_api/generator.rb +16 -5
- data/lib/open_api/version.rb +1 -1
- data/zero-rails_openapi.gemspec +3 -9
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b5be5f5f85b9f720f9fafaa70ba037879c336e0
|
4
|
+
data.tar.gz: fc93ab5059ff2ff594e080e51cb349a9a1ffd64c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0f37a9cb0e11cb4850819e6ed443c6f6b2386ec0f2b47878eb16b1a375e2c50fb8c6b0a2cea3446010eb3b07bc7d709f6aa74b0773d23e88962c3037222e734
|
7
|
+
data.tar.gz: 0db4a7fdc0f13cb55b654ff1362d39a67c399ab5f8ffa0e40eabad9c3f4776042a77c2ac189af281de0208d274d441276c10aeb7fc2b4e486753cc2f0780cae0
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,21 +1,98 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
zero-rails_openapi (1.3.
|
4
|
+
zero-rails_openapi (1.3.3)
|
5
|
+
activesupport (>= 3)
|
6
|
+
rails (>= 3)
|
5
7
|
|
6
8
|
GEM
|
7
9
|
remote: https://rubygems.org/
|
8
10
|
specs:
|
11
|
+
actioncable (5.1.4)
|
12
|
+
actionpack (= 5.1.4)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (~> 0.6.1)
|
15
|
+
actionmailer (5.1.4)
|
16
|
+
actionpack (= 5.1.4)
|
17
|
+
actionview (= 5.1.4)
|
18
|
+
activejob (= 5.1.4)
|
19
|
+
mail (~> 2.5, >= 2.5.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
actionpack (5.1.4)
|
22
|
+
actionview (= 5.1.4)
|
23
|
+
activesupport (= 5.1.4)
|
24
|
+
rack (~> 2.0)
|
25
|
+
rack-test (>= 0.6.3)
|
26
|
+
rails-dom-testing (~> 2.0)
|
27
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
28
|
+
actionview (5.1.4)
|
29
|
+
activesupport (= 5.1.4)
|
30
|
+
builder (~> 3.1)
|
31
|
+
erubi (~> 1.4)
|
32
|
+
rails-dom-testing (~> 2.0)
|
33
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
34
|
+
activejob (5.1.4)
|
35
|
+
activesupport (= 5.1.4)
|
36
|
+
globalid (>= 0.3.6)
|
37
|
+
activemodel (5.1.4)
|
38
|
+
activesupport (= 5.1.4)
|
39
|
+
activerecord (5.1.4)
|
40
|
+
activemodel (= 5.1.4)
|
41
|
+
activesupport (= 5.1.4)
|
42
|
+
arel (~> 8.0)
|
9
43
|
activesupport (5.1.4)
|
10
44
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
11
45
|
i18n (~> 0.7)
|
12
46
|
minitest (~> 5.1)
|
13
47
|
tzinfo (~> 1.1)
|
48
|
+
arel (8.0.0)
|
49
|
+
builder (3.2.3)
|
14
50
|
concurrent-ruby (1.0.5)
|
51
|
+
crass (1.0.2)
|
15
52
|
diff-lcs (1.3)
|
16
|
-
|
53
|
+
erubi (1.7.0)
|
54
|
+
globalid (0.4.1)
|
55
|
+
activesupport (>= 4.2.0)
|
56
|
+
i18n (0.9.1)
|
17
57
|
concurrent-ruby (~> 1.0)
|
58
|
+
loofah (2.1.1)
|
59
|
+
crass (~> 1.0.2)
|
60
|
+
nokogiri (>= 1.5.9)
|
61
|
+
mail (2.7.0)
|
62
|
+
mini_mime (>= 0.1.1)
|
63
|
+
method_source (0.9.0)
|
64
|
+
mini_mime (0.1.4)
|
65
|
+
mini_portile2 (2.3.0)
|
18
66
|
minitest (5.10.3)
|
67
|
+
nio4r (2.1.0)
|
68
|
+
nokogiri (1.8.1)
|
69
|
+
mini_portile2 (~> 2.3.0)
|
70
|
+
rack (2.0.3)
|
71
|
+
rack-test (0.7.0)
|
72
|
+
rack (>= 1.0, < 3)
|
73
|
+
rails (5.1.4)
|
74
|
+
actioncable (= 5.1.4)
|
75
|
+
actionmailer (= 5.1.4)
|
76
|
+
actionpack (= 5.1.4)
|
77
|
+
actionview (= 5.1.4)
|
78
|
+
activejob (= 5.1.4)
|
79
|
+
activemodel (= 5.1.4)
|
80
|
+
activerecord (= 5.1.4)
|
81
|
+
activesupport (= 5.1.4)
|
82
|
+
bundler (>= 1.3.0)
|
83
|
+
railties (= 5.1.4)
|
84
|
+
sprockets-rails (>= 2.0.0)
|
85
|
+
rails-dom-testing (2.0.3)
|
86
|
+
activesupport (>= 4.2.0)
|
87
|
+
nokogiri (>= 1.6)
|
88
|
+
rails-html-sanitizer (1.0.3)
|
89
|
+
loofah (~> 2.0)
|
90
|
+
railties (5.1.4)
|
91
|
+
actionpack (= 5.1.4)
|
92
|
+
activesupport (= 5.1.4)
|
93
|
+
method_source
|
94
|
+
rake (>= 0.8.7)
|
95
|
+
thor (>= 0.18.1, < 2.0)
|
19
96
|
rake (10.5.0)
|
20
97
|
rspec (3.6.0)
|
21
98
|
rspec-core (~> 3.6.0)
|
@@ -30,15 +107,25 @@ GEM
|
|
30
107
|
diff-lcs (>= 1.2.0, < 2.0)
|
31
108
|
rspec-support (~> 3.6.0)
|
32
109
|
rspec-support (3.6.0)
|
110
|
+
sprockets (3.7.1)
|
111
|
+
concurrent-ruby (~> 1.0)
|
112
|
+
rack (> 1, < 3)
|
113
|
+
sprockets-rails (3.2.1)
|
114
|
+
actionpack (>= 4.0)
|
115
|
+
activesupport (>= 4.0)
|
116
|
+
sprockets (>= 3.0.0)
|
117
|
+
thor (0.20.0)
|
33
118
|
thread_safe (0.3.6)
|
34
119
|
tzinfo (1.2.4)
|
35
120
|
thread_safe (~> 0.1)
|
121
|
+
websocket-driver (0.6.5)
|
122
|
+
websocket-extensions (>= 0.1.0)
|
123
|
+
websocket-extensions (0.1.2)
|
36
124
|
|
37
125
|
PLATFORMS
|
38
126
|
ruby
|
39
127
|
|
40
128
|
DEPENDENCIES
|
41
|
-
activesupport
|
42
129
|
bundler (~> 1.16.a)
|
43
130
|
rake (~> 10.0)
|
44
131
|
rspec (~> 3.0)
|
data/README.md
CHANGED
@@ -1,30 +1,29 @@
|
|
1
|
-
# ZRO: OpenApi 3
|
1
|
+
# ZRO: OpenApi 3 JSON-Doc Generator for Rails
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/zero-rails_openapi)
|
4
|
-
[](https://travis-ci.org/zhandao/zero-rails_openapi)
|
5
|
-
|
6
|
-
Provide concise DSL for generating the OpenAPI Specification 3 (**OAS3**, formerly Swagger3) documentation JSON file for Rails application,
|
7
|
-
then you can use Swagger UI 3.2.0+ to show the documentation.
|
3
|
+
[](https://badge.fury.io/rb/zero-rails_openapi)
|
4
|
+
[](https://travis-ci.org/zhandao/zero-rails_openapi)
|
5
|
+
|
6
|
+
Provide concise DSL for generating the OpenAPI Specification 3 (**OAS3**, formerly Swagger3) documentation JSON file for Rails application,
|
7
|
+
then you can use Swagger UI 3.2.0+ to show the documentation.
|
8
8
|
|
9
9
|
## Contributing
|
10
10
|
|
11
|
-
**Hi, here is ZhanDao. This gem was completed when I learned Ruby less than three months,
|
12
|
-
I'm not sure if it has problem, but it may have a lot to improve.
|
13
|
-
I'm looking forward to your issues and PRs, thanks!**
|
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. This gem was completed when I learned Ruby less than three months,
|
12
|
+
I'm not sure if it has problem, but it may have a lot to improve.
|
13
|
+
I'm looking forward to your issues and PRs, thanks!**
|
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 = ▽ =
|
17
17
|
|
18
18
|
## Table of Contents
|
19
19
|
|
20
20
|
- [About OAS](#about-oas) (OpenAPI Specification)
|
21
21
|
- [Installation](#installation)
|
22
22
|
- [Configure](#configure)
|
23
|
-
- [Usage](#usage)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- [Tricks](#tricks)
|
23
|
+
- [Usage - DSL](#usage---dsl)
|
24
|
+
- [Usage - Generate JSON documentation file](#usage---generate-json-documentation-file)
|
25
|
+
- [Usage - Use Swagger UI(very beautiful web page) to show your Documentation](#usage---use-swagger-uivery-beautiful-web-page-to-show-your-documentation)
|
26
|
+
- [Tricks](#tricks)
|
28
27
|
- [Write DSL somewhere else](#trick1---write-the-dsl-somewhere-else)
|
29
28
|
- [Global DRYing](#trick2---global-drying)
|
30
29
|
- [Auto generate description](#trick3---auto-generate-description)
|
@@ -34,310 +33,352 @@ but I dont have enough time now = ▽ =
|
|
34
33
|
|
35
34
|
## About OAS
|
36
35
|
|
37
|
-
Everything about OAS3 is on [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md)
|
38
|
-
|
39
|
-
You can getting started from [swagger.io](https://swagger.io/docs/specification/basic-structure/)
|
40
|
-
|
41
|
-
**I suggest you should understand OAS3's basic structure at least.**
|
42
|
-
such as component (can help you reuse DSL code, when your apis are used with the
|
43
|
-
same data structure).
|
36
|
+
Everything about OAS3 is on [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md)
|
37
|
+
|
38
|
+
You can getting started from [swagger.io](https://swagger.io/docs/specification/basic-structure/)
|
39
|
+
|
40
|
+
**I suggest you should understand OAS3's basic structure at least.**
|
41
|
+
such as component (can help you reuse DSL code, when your apis are used with the
|
42
|
+
same data structure).
|
44
43
|
|
45
44
|
## Installation
|
46
45
|
|
47
|
-
Add this line to your Rails's Gemfile:
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
gem 'zero-rails_openapi'
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
46
|
+
Add this line to your Rails's Gemfile:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
gem 'zero-rails_openapi'
|
50
|
+
# or
|
51
|
+
gem 'zero-rails_openapi', github: 'zhandao/zero-rails_openapi'
|
52
|
+
```
|
53
|
+
|
54
|
+
And then execute:
|
55
|
+
|
56
|
+
$ bundle
|
57
|
+
|
58
|
+
Or install it yourself as:
|
59
|
+
|
60
|
+
$ gem install zero-rails_openapi
|
60
61
|
|
61
62
|
## Configure
|
62
63
|
|
63
|
-
Create an initializer, configure ZRO and define your APIs.
|
64
|
-
|
65
|
-
This is the simplest configuration example:
|
66
|
-
|
67
|
-
```ruby
|
68
|
-
# config/initializers/open_api.rb
|
69
|
-
require 'open_api'
|
70
|
-
|
71
|
-
OpenApi::Config.tap do |c|
|
72
|
-
# [REQUIRED] The output location where .json doc file will be written to.
|
73
|
-
c.file_output_path = 'public/open_api'
|
74
|
-
|
75
|
-
c.register_docs = {
|
76
|
-
homepage_api: {
|
77
|
-
# [REQUIRED] ZRO will scan all the descendants of root_controller, then generate their docs.
|
78
|
-
root_controller: Api::V1::BaseController,
|
79
|
-
|
80
|
-
# [REQUIRED] OAS Info Object: The section contains API information.
|
81
|
-
info: {
|
82
|
-
# [REQUIRED] The title of the application.
|
83
|
-
title: 'Rails APIs',
|
84
|
-
# Description of the application.
|
85
|
-
description: 'API documentation of Rails Application. <br/>' \
|
86
|
-
'Optional multiline or single-line Markdown-formatted description ' \
|
87
|
-
'in [CommonMark](http://spec.commonmark.org/) or `HTML`.',
|
88
|
-
# [REQUIRED] The version of the OpenAPI document
|
89
|
-
# (which is distinct from the OAS version or the API implementation version).
|
90
|
-
version: '1.0.0'
|
91
|
-
}
|
92
|
-
}
|
93
|
-
}
|
94
|
-
end
|
95
|
-
```
|
96
|
-
You can also set the *global configuration(/component)* of OAS:
|
97
|
-
Server Object / Security Scheme Object / Security Requirement Object ...
|
98
|
-
|
99
|
-
For more detailed configuration: [open_api.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/open_api.rb)
|
100
|
-
|
101
|
-
## Usage
|
102
|
-
|
103
|
-
### DSL for documenting your controller
|
104
|
-
|
105
|
-
#### \> First of all, extend DSL for your base controller, for example:
|
106
|
-
|
107
|
-
```ruby
|
108
|
-
# application_controller.rb
|
109
|
-
class ApplicationController < ActionController::API
|
110
|
-
include OpenApi::DSL
|
111
|
-
end
|
112
|
-
```
|
113
|
-
|
114
|
-
#### \> [DSL Usage Example](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/examples_controller.rb)
|
115
|
-
|
116
|
-
(TODO: I consider that, here should be put in a the simplest case.)
|
117
|
-
```ruby
|
118
|
-
class Api::V1::ExamplesController < Api::V1::BaseController
|
119
|
-
apis_tag name: 'ExampleTagName', desc: 'ExamplesController\'s APIs'
|
64
|
+
Create an initializer, configure ZRO and define your APIs.
|
120
65
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
66
|
+
This is the simplest configuration example:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# config/initializers/open_api.rb
|
70
|
+
require 'open_api'
|
71
|
+
|
72
|
+
OpenApi::Config.tap do |c|
|
73
|
+
# [REQUIRED] The output location where .json doc file will be written to.
|
74
|
+
c.file_output_path = 'public/open_api'
|
75
|
+
|
76
|
+
c.register_docs = {
|
77
|
+
homepage_api: {
|
78
|
+
# [REQUIRED] ZRO will scan all the descendants of root_controller, then generate their docs.
|
79
|
+
root_controller: Api::V1::BaseController,
|
80
|
+
|
81
|
+
# [REQUIRED] OAS Info Object: The section contains API information.
|
82
|
+
info: {
|
83
|
+
# [REQUIRED] The title of the application.
|
84
|
+
title: 'Homepage APIs',
|
85
|
+
# Description of the application.
|
86
|
+
description: 'API documentation of Rails Application. <br/>' \
|
87
|
+
'Optional multiline or single-line Markdown-formatted description ' \
|
88
|
+
'in [CommonMark](http://spec.commonmark.org/) or `HTML`.',
|
89
|
+
# [REQUIRED] The version of the OpenAPI document
|
90
|
+
# (which is distinct from the OAS version or the API implementation version).
|
91
|
+
version: '1.0.0'
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
127
95
|
end
|
128
|
-
|
129
|
-
|
130
|
-
|
96
|
+
```
|
97
|
+
The following global configuration and component of OAS are allow to be set in the initializer:
|
98
|
+
Server Object / Security Scheme Object / Security Requirement Object ...
|
99
|
+
|
100
|
+
In addition to direct use of Hash, you can also use Config DSL to configure:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
# config/initializers/open_api.rb
|
104
|
+
require 'open_api'
|
105
|
+
|
106
|
+
OpenApi::Config.tap do |c|
|
107
|
+
c.instance_eval do
|
108
|
+
api :homepage_api, root_controller: ApiDoc
|
109
|
+
info version: '1.0.0', title: 'Homepage APIs'
|
110
|
+
end
|
131
111
|
end
|
112
|
+
```
|
113
|
+
|
114
|
+
For more detailed configuration: [open_api.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/open_api.rb)
|
115
|
+
See all the settings you can configure: [config.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/config.rb)
|
132
116
|
|
133
|
-
|
134
|
-
this_api_is_invalid! 'this api is expired!'
|
135
|
-
desc 'Optional multiline or single-line Markdown-formatted description',
|
136
|
-
id: 'user id',
|
137
|
-
email_addr: 'email_addr\'s desc'
|
138
|
-
email = 'git@github.com'
|
139
|
-
|
140
|
-
query! :id, Integer, enum: 0..5, length: [1, 2], pattern: /^[0-9]$/, range: {gt:0, le:5}
|
141
|
-
query! :done, Boolean, must_be: false, default: true, desc: 'must be false'
|
142
|
-
query :email_addr, String, lth: :ge_3, default: email # is_a: :email
|
143
|
-
# form! 'form', type: { id!: Integer, name: String }
|
144
|
-
file :pdf, 'desc: the media type is application/pdf'
|
145
|
-
|
146
|
-
response :success, 'success response', :json, type: :Dog
|
147
|
-
|
148
|
-
security :ApiKeyAuth
|
149
|
-
end
|
117
|
+
## Usage - DSL
|
150
118
|
|
151
|
-
|
152
|
-
|
153
|
-
|
119
|
+
### First of all, include DSL to your base controller (which is for writing docs), for example:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# app/controllers/api/api_controller.rb
|
123
|
+
class ApiController < ActionController::API
|
124
|
+
include OpenApi::DSL
|
154
125
|
end
|
155
|
-
|
126
|
+
```
|
156
127
|
|
157
|
-
|
128
|
+
### DSL Usage Example
|
158
129
|
|
159
|
-
|
130
|
+
Here is the simplest usage:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
class Api::V1::ExamplesController < Api::V1::BaseController
|
134
|
+
open_api :index, 'GET list' do
|
135
|
+
query :page, Integer#, desc: 'page, greater than 1', range: { ge: 1 }, dft: 1
|
136
|
+
query :rows, Integer#, desc: 'per page', range: { ge: 1 }, default: 10
|
137
|
+
end
|
138
|
+
end
|
139
|
+
```
|
140
|
+
|
141
|
+
For more example, see [goods_doc.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/goods_doc.rb), and
|
142
|
+
[examples_controller.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/examples_controller.rb)
|
160
143
|
|
161
|
-
|
144
|
+
### DSL methods of controller ([source code](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl.rb))
|
162
145
|
|
163
|
-
|
146
|
+
#### (1) `ctrl_path` (controller path) [optional]
|
164
147
|
|
165
148
|
```ruby
|
166
149
|
# method signature
|
167
|
-
ctrl_path
|
150
|
+
ctrl_path(path)
|
168
151
|
# usage
|
169
152
|
ctrl_path 'api/v1/examples'
|
170
153
|
```
|
171
|
-
|
154
|
+
It is optional because `ctrl_path` defaults to `controller_path`.
|
155
|
+
|
172
156
|
[Here's a trick](#trick1---write-the-dsl-somewhere-else): Using `ctrl_path`, you can write the DSL somewhere else
|
173
157
|
to simplify the current controller.
|
174
|
-
\* take the tag from `path.split('/').last`
|
175
158
|
|
176
|
-
|
159
|
+
#### (2) `apis_tag` [optional]
|
177
160
|
|
178
161
|
```ruby
|
179
162
|
# method signature
|
180
|
-
apis_tag
|
163
|
+
apis_tag(name: nil, desc: '', external_doc_url: '')
|
181
164
|
# usage
|
182
165
|
apis_tag name: 'ExampleTagName', desc: 'ExamplesController\'s APIs'
|
183
166
|
```
|
184
|
-
|
167
|
+
This method allows you to set the Tag (which is a node of [OpenApi Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#openapi-object)).
|
168
|
+
|
169
|
+
desc and external_doc_url will be output to the tags[the current tag] (tag defaults to controller_name), but are optional.
|
185
170
|
|
186
|
-
|
171
|
+
#### (3) `components` [optional]
|
187
172
|
|
188
173
|
```ruby
|
189
174
|
# method signature
|
190
|
-
components
|
175
|
+
components(&block)
|
191
176
|
# usage
|
192
177
|
components do
|
193
178
|
# DSL for defining components
|
194
|
-
schema :
|
179
|
+
schema :DogSchema => [ { id: Integer, name: String }, dft: { id: 1, name: 'pet' } ]
|
180
|
+
query! :UidQuery => [ :uid, String, desc: 'uid' ]
|
181
|
+
resp :BadRqResp => [ 'bad request', :json ]
|
182
|
+
end
|
183
|
+
|
184
|
+
# to use component
|
185
|
+
open_api :action, 'summary' do
|
186
|
+
query :doge, :DogSchema # to use a Schema component
|
187
|
+
param_ref :UidQuery # to use a Parameter component
|
188
|
+
response_ref :BadRqResp # to use a Response component
|
195
189
|
end
|
196
190
|
```
|
197
|
-
Component can be used to simplify your DSL code
|
198
|
-
|
191
|
+
Component can be used to simplify your DSL code (by using `*_ref` methods).
|
192
|
+
|
193
|
+
Each RefObj you defined is associated with components through component key.
|
194
|
+
We suggest that component keys should be camelized symbol.
|
199
195
|
|
200
|
-
|
196
|
+
#### (4) `api_dry` [optional]
|
201
197
|
|
202
|
-
|
198
|
+
This method is for DRYing.
|
203
199
|
|
204
200
|
```ruby
|
205
201
|
# method signature
|
206
|
-
api_dry
|
202
|
+
api_dry(action = :all, desc = '', &block)
|
207
203
|
# usage
|
208
|
-
api_dry :all, 'common response'
|
209
|
-
api_dry :index
|
210
|
-
api_dry [:index, :show] do
|
204
|
+
api_dry :all, 'common response' # block ...
|
205
|
+
api_dry :index # block ...
|
206
|
+
api_dry [:index, :show] do
|
207
|
+
query! #...
|
208
|
+
end
|
211
209
|
```
|
212
210
|
|
213
|
-
As you think, the
|
211
|
+
As you think, the block will be executed to each specified API(action) **firstly**.
|
214
212
|
|
215
|
-
|
213
|
+
#### (5) `open_api` [required]
|
216
214
|
|
217
|
-
Define the specified API (in the following example is index).
|
215
|
+
Define the specified API (controller action, in the following example is index).
|
218
216
|
|
219
217
|
```ruby
|
220
218
|
# method signature
|
221
|
-
open_api
|
219
|
+
open_api(action, summary = '', builder: nil, skip: [ ], use: [ ], &block)
|
222
220
|
# usage
|
223
|
-
open_api :index, '(SUMMARY) this api blah blah ...', builder: :
|
221
|
+
open_api :index, '(SUMMARY) this api blah blah ...', builder: :index # block ...
|
222
|
+
```
|
223
|
+
If you pass `builder`, and `generate_jbuilder_file` is set to `true` (in your initializer),
|
224
|
+
ZRO will generate JBuilder file by using specified template called `index`.
|
225
|
+
About template settings, see: [open_api.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/open_api.rb)
|
226
|
+
|
227
|
+
`use` and `skip` options: to use or skip the parameters defined in `api_dry`.
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
open_api :show, 'summary', use: [:id] # => it will only take :id from DRYed result.
|
224
231
|
```
|
225
|
-
If pass `builder` to the third parameter,
|
226
|
-
and `generate_jbuilder_file` is set `true` in your initializer file,
|
227
|
-
ZRO will generate JBuilder file by using specified template that you set
|
228
|
-
`template1` in your setting file.
|
229
|
-
You can see: [open_api.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/open_api.rb)
|
230
|
-
|
231
|
-
|
232
|
-
#### \>\> DSL methods inside *open_api* and *api_dry*'s block ([source code](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl_inside_block.rb):: ApiInfoObj)
|
233
232
|
|
234
|
-
|
235
|
-
parameters, request body, responses, securities, servers.
|
233
|
+
### DSL methods inside [open_api]() and [api_dry]()'s block
|
236
234
|
|
237
|
-
|
235
|
+
[source code](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl_inside_block.rb), ApiInfoObj
|
236
|
+
|
237
|
+
These following methods in the block describe the specified API action: description, valid?,
|
238
|
+
parameters, request body, responses, securities, servers.
|
239
|
+
|
240
|
+
(Here corresponds to OAS [Operation Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#operationObject))
|
238
241
|
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
242
|
+
#### (1) `this_api_is_invalid!`, its aliases:
|
243
|
+
```
|
244
|
+
this_api_is_expired!
|
245
|
+
this_api_is_unused!
|
246
|
+
this_api_is_under_repair!
|
247
|
+
```
|
243
248
|
|
244
249
|
```ruby
|
245
250
|
# method signature
|
246
|
-
this_api_is_invalid!
|
251
|
+
this_api_is_invalid!(explain = '')
|
247
252
|
# usage
|
248
253
|
this_api_is_invalid! 'this api is expired!'
|
249
254
|
```
|
255
|
+
|
256
|
+
Then `deprecated` of this API will be set to true.
|
250
257
|
|
251
|
-
|
258
|
+
#### (2) `desc`: description for the current API and its inputs (parameters and request body)
|
252
259
|
|
253
260
|
```ruby
|
254
261
|
# method signature
|
255
|
-
desc
|
262
|
+
desc(desc, param_descs = { })
|
256
263
|
# usage
|
257
264
|
desc 'current API\'s description',
|
258
|
-
id:
|
259
|
-
|
265
|
+
id: 'desc of the parameter :id',
|
266
|
+
email: 'desc of the parameter :email'
|
260
267
|
```
|
261
268
|
|
262
|
-
You can of course describe the input in it's DSL method (like `query! :done
|
269
|
+
You can of course describe the input in it's DSL method (like `query! :done ...`, [this line](https://github.com/zhandao/zero-rails_openapi#-dsl-usage-example)),
|
263
270
|
but that will make it long and ugly. We recommend that unite descriptions in this place.
|
264
271
|
|
265
272
|
In addition, when you want to dry the same parameters (each with a different description), it will be of great use.
|
266
273
|
|
267
|
-
|
268
|
-
- `param`
|
269
|
-
- `param_ref`
|
270
|
-
- `header`, `path`, `query`, `cookie` and bang methods: `header!`, `path!`, `query!`, `cookie!`
|
271
|
-
- `do_* by: { }`
|
272
|
-
**The bang method(`!`) means it is required, so without `!` means it is optional. the same below.**
|
274
|
+
#### (3) `param` family methods (OAS - [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#parameterObject))
|
273
275
|
|
274
|
-
Define the parameters for the API(
|
275
|
-
|
276
|
+
Define the parameters for the API (action).
|
277
|
+
```
|
278
|
+
param
|
279
|
+
param_ref # for reuse component,
|
280
|
+
# it links sepcified RefObjs (by component keys) to current parameters.
|
281
|
+
header, path, query, cookie # will pass specified parameter location to `param`
|
282
|
+
header!, path!, query!, cookie! # bang method of above methods
|
283
|
+
do_* by: { parameter_definations } # batch definition parameters, such as do_path, do_query
|
284
|
+
order # order parameters by names array you passed
|
285
|
+
examples # define examples of parameters
|
286
|
+
```
|
287
|
+
**The bang method (which's name is end of a exclamation point `!`) means this param is required, so without `!` means optional.**
|
288
|
+
**THE SAME BELOW.**
|
276
289
|
|
277
290
|
```ruby
|
291
|
+
# `param_type` just is the location of parameter, like: query, path
|
292
|
+
# `schema_type` is the type of parameter, like: String, Integer (must be a constant)
|
293
|
+
# For more explanation, please click the link below ↓↓↓
|
278
294
|
# method signature
|
279
|
-
param
|
295
|
+
param(param_type, param_name, schema_type, is_required, schema_hash = { })
|
280
296
|
# usage
|
281
|
-
param :query, :page, Integer, :req, range: { gt: 0, le: 5 }, desc: 'page'
|
282
|
-
|
297
|
+
param :query, :page, Integer, :req, range: { gt: 0, le: 5 }, desc: 'page'
|
298
|
+
|
283
299
|
|
284
300
|
# method signature
|
285
|
-
param_ref
|
301
|
+
param_ref(component_key, *component_keys) # should pass at least 1 key
|
286
302
|
# usage
|
287
|
-
param_ref :
|
288
|
-
param_ref :
|
303
|
+
param_ref :IdPath
|
304
|
+
param_ref :IdPath, :NameQuery, :TokenHeader
|
305
|
+
|
289
306
|
|
290
307
|
# method signature
|
291
|
-
|
292
|
-
header!
|
293
|
-
|
308
|
+
header(param_name, schema_type, schema_hash = { })
|
309
|
+
header!(param_name, schema_type, schema_hash = { })
|
310
|
+
query!(param_name, schema_type, schema_hash = { })
|
294
311
|
# usage
|
295
312
|
header! 'Token', String
|
296
|
-
query! :
|
313
|
+
query! :readed, Boolean, must_be: true, default: false
|
314
|
+
# The same effect as above, but not simple
|
315
|
+
param :query, :readed, Boolean, :req, must_be: true, default: false
|
316
|
+
|
297
317
|
|
298
318
|
# method signature
|
299
|
-
do_query
|
319
|
+
do_query(by:)
|
300
320
|
# usage
|
301
321
|
do_query by: {
|
302
|
-
|
303
|
-
|
322
|
+
search_type: String,
|
323
|
+
search_val: String,
|
324
|
+
export!: Boolean
|
304
325
|
}
|
305
|
-
#
|
326
|
+
# The same effect as above, but a little bit repetitive
|
306
327
|
query :search_type, String
|
328
|
+
query :search_val, String
|
307
329
|
query! :export, Boolean
|
330
|
+
|
331
|
+
|
332
|
+
# method signature
|
333
|
+
# `exp_by` (select_example_by): choose the example fields.
|
334
|
+
examples(exp_by = :all, examples_hash)
|
335
|
+
# usage
|
336
|
+
# it defines 2 examples by using parameter :id and :name
|
337
|
+
# if pass :all to `exp_by`, keys will be all the parameter's names.
|
338
|
+
examples [:id, :name], {
|
339
|
+
:right_input => [ 1, 'user'], # == { id: 1, name: 'user' }
|
340
|
+
:wrong_input => [ -1, '' ]
|
341
|
+
}
|
308
342
|
```
|
309
343
|
|
310
|
-
[**>> More About
|
344
|
+
[**>> More About `param` DSL <<**](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/parameter.md)
|
311
345
|
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
346
|
+
#### (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
|
+
|
348
|
+
OpenAPI 3.0 uses the requestBody keyword to distinguish the payload from parameters.
|
349
|
+
```
|
350
|
+
request_body
|
351
|
+
body_ref # for reuse component,
|
352
|
+
# it links sepcified RefObjs (by component keys) to current body.
|
353
|
+
body, body! # alias of request_body
|
354
|
+
form, form! # define a multipart/form-data body
|
355
|
+
file, file! # define a File media-type body
|
356
|
+
```
|
321
357
|
|
322
358
|
```ruby
|
323
359
|
# method signature
|
324
|
-
request_body
|
360
|
+
request_body(is_required, media_type, desc = '', schema_hash = { })
|
325
361
|
# usage
|
326
|
-
request_body :opt, :form, type: { id!: Integer, name: String }
|
362
|
+
request_body :opt, :form, '', type: { id!: Integer, name: String }
|
363
|
+
# or
|
364
|
+
request_body :opt, :form, '', data: { id!: Integer, name: String }
|
365
|
+
|
327
366
|
|
328
367
|
# method signature
|
329
|
-
body_ref
|
368
|
+
body_ref(component_key)
|
330
369
|
# usage
|
331
|
-
body_ref :
|
370
|
+
body_ref :UpdateDogeBody
|
371
|
+
|
332
372
|
|
333
373
|
# method signature
|
334
|
-
body!
|
374
|
+
body!(media_type, desc = '', schema_hash = { })
|
335
375
|
# usage
|
336
376
|
body :json
|
337
377
|
|
378
|
+
|
338
379
|
# method implement
|
339
|
-
def form desc = '',
|
340
|
-
body :form, desc,
|
380
|
+
def form desc = '', schema_hash = { }
|
381
|
+
body :form, desc, schema_hash
|
341
382
|
end
|
342
383
|
# usage
|
343
384
|
form! 'register', data: {
|
@@ -347,197 +388,208 @@ parameters, request body, responses, securities, servers.
|
|
347
388
|
}
|
348
389
|
# advance usage
|
349
390
|
form 'for creating a user', data: {
|
350
|
-
|
391
|
+
:name! => { type: String, desc: 'user name' },
|
351
392
|
:password! => { type: String, pattern: /[0-9]{6,10}/, desc: 'password' },
|
352
393
|
# optional
|
353
|
-
|
354
|
-
}
|
394
|
+
:remarks => { type: String, desc: 'remarks' },
|
395
|
+
}, exp_by: %i[ name password ],
|
396
|
+
examples: { # ↓ ↓
|
397
|
+
:right_input => [ 'user1', '123456' ],
|
398
|
+
:wrong_input => [ 'user2', 'abc' ]
|
399
|
+
}
|
355
400
|
|
356
|
-
|
357
|
-
|
358
|
-
|
401
|
+
|
402
|
+
# about `file`
|
403
|
+
def file! media_type, desc = '', schema_hash = { type: File }
|
404
|
+
body! media_type, desc, schema_hash
|
359
405
|
end
|
360
406
|
```
|
361
407
|
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
408
|
+
1. **Notice:** Each API should only declare a request body
|
409
|
+
That is, all of the above methods you can only choose one of them.
|
410
|
+
(But **multiple media types** will be supported in the future).
|
411
|
+
2. `media_type`: we provide some [mapping](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/oas_objs/media_type_obj.rb) from symbols to real media-types.
|
412
|
+
3. `schema_hash`: as above (see param).
|
413
|
+
**One thing that should be noted is: when use Hash writing, `scham_type` is writed in schema_hash using key :type.**
|
414
|
+
4. `exp_by` and `examples`: for the above example, the following has the same effect:
|
415
|
+
```
|
416
|
+
examples: {
|
417
|
+
:right_input => { name: 'user1', password: '123456' },
|
418
|
+
:wrong_input => { name: 'user2', password: 'abc' }
|
419
|
+
}
|
420
|
+
```
|
367
421
|
|
368
|
-
|
369
|
-
- `response` (`resp`)
|
370
|
-
- `response_ref`
|
371
|
-
- `default_response` (`dft_resp`)
|
372
|
-
- `error_response` (`other_response`, `oth_resp`, `error`, `err_resp`): Are `response`'s aliases, should be used in the error response context.
|
373
|
-
- `override_response` # TODO
|
422
|
+
#### (5) `response` family methods (OAS - [Response Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#response-object))
|
374
423
|
|
375
|
-
Define the responses for the API(
|
376
|
-
|
424
|
+
Define the responses for the API (action).
|
425
|
+
```
|
426
|
+
response or resp
|
427
|
+
response_ref
|
428
|
+
default_response or dft_resp
|
429
|
+
error_response, other_response, oth_resp, error, err_resp # response's aliases, should be used in the error response context.
|
430
|
+
```
|
377
431
|
|
378
432
|
```ruby
|
379
433
|
# method signature
|
380
|
-
response
|
434
|
+
response(response_code, desc, media_type = nil, schema_hash = { })
|
381
435
|
# usage
|
382
|
-
response 200, 'query result
|
436
|
+
response 200, 'query result', :pdf, type: File
|
383
437
|
|
384
438
|
# method signature
|
385
|
-
response_ref
|
439
|
+
response_ref(code_compkey_hash)
|
386
440
|
# usage
|
387
|
-
response_ref 700 => :
|
441
|
+
response_ref 700 => :AResp, 800 => :BResp
|
388
442
|
```
|
389
443
|
|
390
|
-
|
391
|
-
(2) `examples` usage see [goods_doc](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/goods_doc.rb)
|
444
|
+
**practice:** Combined with wrong class, automatically generate error responses. [AutoGenDoc](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/auto_gen_doc.rb#L63)
|
392
445
|
|
393
|
-
|
446
|
+
#### (6) `security`: TODO
|
394
447
|
|
395
|
-
|
448
|
+
#### (7) `server`: TODO
|
396
449
|
|
397
|
-
|
450
|
+
### DSL methods inside [components]()'block ([code source](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl_inside_block.rb):: CtrlInfoObj )
|
398
451
|
|
399
|
-
(Here corresponds to OAS [Components Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#componentsObject))
|
400
|
-
|
401
|
-
These methods in the block describe
|
402
|
-
|
403
|
-
|
404
|
-
So, the following methods are used as above, except that you need to
|
405
|
-
name the key of the component, and pass it as the first of argument list, like:
|
406
|
-
|
407
|
-
```ruby
|
408
|
-
query! :QueryCompUuid, :product_uuid, Integer, ...
|
409
|
-
```
|
410
|
-
This writing is feasible but not recommended,
|
411
|
-
because component's key and parameter's name looks like the same level.
|
412
|
-
The recommended writing is:
|
452
|
+
(Here corresponds to OAS [Components Object](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md#componentsObject))
|
453
|
+
|
454
|
+
These methods in the block describe reusable components that you wanna use to define
|
455
|
+
parameter, response, request body and schema.
|
413
456
|
|
414
|
-
```ruby
|
415
|
-
query! :
|
416
|
-
```
|
417
|
-
|
457
|
+
```ruby
|
458
|
+
query! :UidQuery, :uid, String
|
459
|
+
```
|
460
|
+
This writing is feasible but not recommended,
|
461
|
+
because component's key and parameter's name looks like the same level.
|
462
|
+
The recommended writing is:
|
418
463
|
|
419
|
-
|
420
|
-
|
421
|
-
|
464
|
+
```ruby
|
465
|
+
query! :UidQuery => [:uid, String]
|
466
|
+
```
|
422
467
|
|
423
468
|
```ruby
|
424
469
|
# method signature
|
425
|
-
schema
|
470
|
+
schema(component_key, type, schema_hash)
|
426
471
|
# usage
|
427
|
-
schema :Dog => [
|
472
|
+
schema :Dog => [ String, desc: 'dogee' ]
|
428
473
|
# advance usage
|
429
474
|
schema :Dog => [{
|
430
475
|
id!: Integer,
|
431
|
-
name: { type: String, must_be: '
|
432
|
-
}, # this is schema type
|
433
|
-
dft: { id: 1, name: 'pet' }
|
476
|
+
name: { type: String, must_be: 'name', desc: 'name' }
|
477
|
+
}, # <= this hash is schema type[1]
|
478
|
+
dft: { id: 1, name: 'pet' },
|
479
|
+
desc: 'dogee']
|
434
480
|
# or (unrecommended)
|
435
|
-
schema :Dog, { id!: Integer, name: String }, dft: { id: 1, name: 'pet' }
|
481
|
+
schema :Dog, { id!: Integer, name: String }, dft: { id: 1, name: 'pet' }, desc: 'dogee'
|
436
482
|
```
|
437
|
-
|
438
|
-
|
439
|
-
### Generate JSON Documentation File
|
440
|
-
|
441
|
-
Initializer or Console, run:
|
483
|
+
[1] see: [Type](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/parameter.md#type)
|
442
484
|
|
443
|
-
|
444
|
-
OpenApi.write_docs
|
445
|
-
```
|
485
|
+
## Usage - Generate JSON Documentation File
|
446
486
|
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
#### Trick1 - Write the DSL Somewhere Else
|
487
|
+
Use `OpenApi.write_docs`:
|
488
|
+
|
489
|
+
```ruby
|
490
|
+
# initializer
|
491
|
+
OpenApi.write_docs generate_files: !Rails.env.production?
|
492
|
+
|
493
|
+
# or run directly in console
|
494
|
+
OpenApi.write_docs
|
495
|
+
```
|
496
|
+
|
497
|
+
Then the JSON files will be written to the directories you set. (Each API a file.)
|
460
498
|
|
461
|
-
|
462
|
-
Do you want to separate documentation from business controller to simplify both?
|
463
|
-
Very easy! Just use `ctrl_path`.
|
499
|
+
## Usage - Use Swagger UI(very beautiful web page) to show your Documentation
|
464
500
|
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
501
|
+
Download [Swagger UI](https://github.com/swagger-api/swagger-ui) (version >= 2.3.0 support the OAS3)
|
502
|
+
to your project,
|
503
|
+
modify the default JSON file path(url) in the index.html
|
504
|
+
(window.onload >> SwaggerUIBundle >> url).
|
505
|
+
In order to use it, you may have to enable CORS, [see](https://github.com/swagger-api/swagger-ui#cors-support)
|
469
506
|
|
470
|
-
|
471
|
-
require 'open_api/dsl'
|
507
|
+
## Tricks
|
472
508
|
|
473
|
-
|
474
|
-
include OpenApi::DSL
|
475
|
-
end
|
509
|
+
### Trick1 - Write the DSL Somewhere Else
|
476
510
|
|
477
|
-
|
478
|
-
|
479
|
-
|
511
|
+
Does your documentation take too many lines?
|
512
|
+
Do you want to separate documentation from business controller to simplify both?
|
513
|
+
Very easy! Just follow
|
514
|
+
|
515
|
+
```ruby
|
516
|
+
# config/initializers/open_api.rb
|
517
|
+
# in your configuration
|
518
|
+
root_controller: ApiDoc
|
480
519
|
|
481
|
-
|
482
|
-
|
520
|
+
# app/api_doc/api_doc.rb
|
521
|
+
require 'open_api/dsl'
|
522
|
+
|
523
|
+
class ApiDoc < Object
|
524
|
+
include OpenApi::DSL
|
483
525
|
end
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
#### Trick3 - Auto Generate Description
|
497
|
-
|
498
|
-
```ruby
|
499
|
-
desc 'api desc',
|
500
|
-
search_type!: 'search field, allows:<br/>'
|
501
|
-
query :search_type, String, enum: %w[name creator category price]
|
502
|
-
|
503
|
-
# or
|
504
|
-
|
505
|
-
query :search_type, String, desc!: 'search field, allows:<br/>',
|
506
|
-
enum: %w[name creator category price]
|
507
|
-
```
|
508
|
-
|
509
|
-
Notice `!` use (`search_type!`, `desc!`), it tells ZRO to append
|
510
|
-
information that analyzed from definitions (enum, must_be ..) to description automatically.
|
526
|
+
|
527
|
+
# app/api_doc/v1/examples_doc.rb
|
528
|
+
class V1::ExamplesDoc < ApiDoc
|
529
|
+
ctrl_path 'api/v1/examples'
|
530
|
+
|
531
|
+
open_api :index do
|
532
|
+
# ...
|
533
|
+
end
|
534
|
+
end
|
535
|
+
```
|
536
|
+
|
537
|
+
Notes: convention is the file name ends with `_doc.rb`
|
511
538
|
|
512
|
-
|
513
|
-
> search field, allows:<br/>1/ name<br/>2/ creator,<br/>3/ category<br/>4/ price<br/>
|
539
|
+
### Trick2 - Global DRYing
|
514
540
|
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
'expensive goods': :get,
|
522
|
-
'cheap goods': :borrow,
|
523
|
-
}
|
524
|
-
```
|
525
|
-
Read this [file](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/auto_gen_desc.rb) to learn more.
|
541
|
+
Method `api_dry` is for DRY but its scope is limited to the current controller.
|
542
|
+
|
543
|
+
I have no idea of best practices, But you can look at this [file](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/auto_gen_doc.rb).
|
544
|
+
The implementation of the file is: do `api_dry` when inherits the base controller inside `inherited` method.
|
545
|
+
|
546
|
+
You can use `sort` to specify the order of parameters.
|
526
547
|
|
527
|
-
|
548
|
+
### Trick3 - Auto Generate Description
|
528
549
|
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
550
|
+
```ruby
|
551
|
+
desc 'api desc',
|
552
|
+
search_type!: 'search field, allows:<br/>'
|
553
|
+
query :search_type, String, enum: %w[name creator category price]
|
554
|
+
|
555
|
+
# or
|
556
|
+
|
557
|
+
query :search_type, String, desc!: 'search field, allows:<br/>',
|
558
|
+
enum: %w[name creator category price]
|
559
|
+
```
|
560
|
+
|
561
|
+
Notice `!` use (`search_type!`, `desc!`), it tells ZRO to append
|
562
|
+
information that analyzed from definitions (enum, must_be ..) to description automatically.
|
563
|
+
|
564
|
+
Any one of above will generate:
|
565
|
+
> search field, allows:<br/>1/ name<br/>2/ creator,<br/>3/ category<br/>4/ price<br/>
|
566
|
+
|
567
|
+
You can also use Hash to define `enum`:
|
568
|
+
```ruby
|
569
|
+
query :view, String, desc: 'allows values<br/>', enum: {
|
570
|
+
'all goods (default)': :all,
|
571
|
+
'only online': :online,
|
572
|
+
'only offline': :offline,
|
573
|
+
'expensive goods': :get,
|
574
|
+
'cheap goods': :borrow,
|
575
|
+
}
|
576
|
+
```
|
577
|
+
Read this [file](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/auto_gen_desc.rb) to learn more.
|
533
578
|
|
534
|
-
|
579
|
+
### Trick4 - Skip or Use parameters define in api_dry
|
535
580
|
|
536
|
-
|
581
|
+
Pass `skip: []` and `use: []` to `open_api` like following code:
|
582
|
+
```ruby
|
583
|
+
open_api :index, 'desc', builder: :index, skip: [ :Token ]
|
584
|
+
```
|
585
|
+
|
586
|
+
Look at this [file](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/goods_doc.rb) to learn more.
|
537
587
|
|
538
|
-
|
588
|
+
### Trick5 - Auto Generate index/show Actions's Response-Types Based on DB Schema
|
539
589
|
|
540
|
-
|
590
|
+
Use method `load_schema` in `api_dry`.
|
591
|
+
|
592
|
+
See this [file](https://github.com/zhandao/zero-rails_openapi/blob/master/documentation/examples/auto_gen_doc.rb#L51) for uasge information.
|
541
593
|
|
542
594
|
|
543
595
|
## Troubleshooting
|
@@ -547,14 +599,14 @@ See this [file](https://github.com/zhandao/zero-rails_openapi/blob/master/docume
|
|
547
599
|
|
548
600
|
## Development
|
549
601
|
|
550
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
551
|
-
|
552
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
602
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
603
|
+
|
604
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
553
605
|
|
554
606
|
## License
|
555
607
|
|
556
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
608
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
557
609
|
|
558
610
|
## Code of Conduct
|
559
611
|
|
560
|
-
Everyone interacting in the Zero-OpenApi project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/zhandao/zero-rails_openapi/blob/master/CODE_OF_CONDUCT.md).
|
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](https://github.com/zhandao/zero-rails_openapi/blob/master/CODE_OF_CONDUCT.md).
|