zero-rails_openapi 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +245 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/examples/examples_controller.rb +36 -0
- data/lib/examples/open_api.rb +87 -0
- data/lib/oas_objs/helpers.rb +41 -0
- data/lib/oas_objs/media_type_obj.rb +86 -0
- data/lib/oas_objs/param_obj.rb +83 -0
- data/lib/oas_objs/ref_obj.rb +29 -0
- data/lib/oas_objs/request_body_obj.rb +54 -0
- data/lib/oas_objs/response_obj.rb +44 -0
- data/lib/oas_objs/schema_obj.rb +187 -0
- data/lib/open_api.rb +9 -0
- data/lib/open_api/config.rb +34 -0
- data/lib/open_api/dsl.rb +58 -0
- data/lib/open_api/dsl_inside_block.rb +175 -0
- data/lib/open_api/generator.rb +75 -0
- data/lib/open_api/version.rb +3 -0
- data/lib/takes/open_api.rake +6 -0
- data/zero-rails_openapi.gemspec +37 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 388bb78c95e92399dacc43b0a83ae3b5165e3ea2
|
4
|
+
data.tar.gz: 352a5d7fdf9f6aa670a7e0ea56a99934dec0375c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 26b8d36f4601facd4ed6936e5be617a8cc03ee279c99c603727ad7fb7432841f2e9ad20691e1d1a0f6e448a837b758f55cf5e4408027b87cc25ce3bd7391a0c4
|
7
|
+
data.tar.gz: bbfc3c14e4c72b8f56eed5dae4fb4df9c5120e81bd1c7d8a4fd535c0853a7e23d018812aabfa3283d51b421b8a8aee6d3ab84ef055087dbf6c418c5a1bb4a82d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at will.huang@dji.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
zero-rails_openapi (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.6.0)
|
12
|
+
rspec-core (~> 3.6.0)
|
13
|
+
rspec-expectations (~> 3.6.0)
|
14
|
+
rspec-mocks (~> 3.6.0)
|
15
|
+
rspec-core (3.6.0)
|
16
|
+
rspec-support (~> 3.6.0)
|
17
|
+
rspec-expectations (3.6.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.6.0)
|
20
|
+
rspec-mocks (3.6.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.6.0)
|
23
|
+
rspec-support (3.6.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16.a)
|
30
|
+
rake (~> 10.0)
|
31
|
+
rspec (~> 3.0)
|
32
|
+
zero-rails_openapi!
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.0.pre.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 will.huang
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,245 @@
|
|
1
|
+
# ZRO: OpenApi 3 DocGenerator for Rails
|
2
|
+
|
3
|
+
Provide concise DSL for you to generate the OpenAPI Specification 3 (**OAS3**, formerly Swagger3) JSON file for Rails application,
|
4
|
+
then you can use Swagger-UI 3.2.0+ to show the documentation.
|
5
|
+
|
6
|
+
## About OAS
|
7
|
+
|
8
|
+
Everything about OAS3 is on [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification/blob/OpenAPI.next/versions/3.0.0.md)
|
9
|
+
|
10
|
+
You can getting started from [swagger.io](https://swagger.io/docs/specification/basic-structure/)
|
11
|
+
|
12
|
+
**I suggest you should understand OAS3's basic structure at least.**
|
13
|
+
such as component (can help you reuse DSL code, when your apis are used with the
|
14
|
+
same data structure).
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your Rails's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'zero-rails_openapi'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install zero-rails_openapi
|
31
|
+
|
32
|
+
## Configure
|
33
|
+
|
34
|
+
Create an initializer, configure ZRO and define your APIs.
|
35
|
+
|
36
|
+
This is the simplest configuration example:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# config/initializers/open_api.rb
|
40
|
+
require 'open_api'
|
41
|
+
|
42
|
+
OpenApi.configure do |c|
|
43
|
+
# [REQUIRED] The output location where .json doc file will be written to.
|
44
|
+
c.file_output_path = 'public/open_api'
|
45
|
+
|
46
|
+
c.register_apis = {
|
47
|
+
homepage_api: {
|
48
|
+
# [REQUIRED] ZRO will scan all the descendants of the root_controller, then generate their docs.
|
49
|
+
root_controller: Api::V1::BaseController,
|
50
|
+
|
51
|
+
# [REQUIRED] OAS Info Object: The section contains API information.
|
52
|
+
info: {
|
53
|
+
# [REQUIRED] The title of the application.
|
54
|
+
title: 'Rails APIs',
|
55
|
+
# Description of the application.
|
56
|
+
description: 'API documentation of Rails Application. <br/>' \
|
57
|
+
'Optional multiline or single-line Markdown-formatted description ' \
|
58
|
+
'in [CommonMark](http://spec.commonmark.org/) or `HTML`.',
|
59
|
+
# [REQUIRED] The version of the OpenAPI document
|
60
|
+
# (which is distinct from the OAS version or the API implementation version).
|
61
|
+
version: '1.0.0'
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
end
|
66
|
+
```
|
67
|
+
You can also set the *global configuration(/component)* of OAS:
|
68
|
+
Server Object / Security Scheme Object / Security Requirement Object ...
|
69
|
+
|
70
|
+
For more detailed configuration: [open_api.rb](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/examples/open_api.rb)
|
71
|
+
|
72
|
+
## Usage
|
73
|
+
|
74
|
+
### DSL for documenting your controller
|
75
|
+
|
76
|
+
#### \> First of all, extend DSL for your base controller, for example:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# application_controller.rb
|
80
|
+
require 'open_api/dsl'
|
81
|
+
|
82
|
+
class ApplicationController < ActionController::API
|
83
|
+
include OpenApi::DSL
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
#### \> [DSL Usage Example](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/examples/examples_controller.rb)
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class Api::V1::ExamplesController < Api::V1::BaseController
|
91
|
+
apis_set 'ExamplesController\'s APIs' do
|
92
|
+
schema :Dog => [ { id!: Integer, name: String }, dft: { id: 1, name: 'pet' } ]
|
93
|
+
path! :PathCompId => [ :id, Integer, desc: 'user id' ]
|
94
|
+
query! :QueryCompUuid => [ :product_uuid, { uuid: String, time: 'int32' }, desc: 'product uuid' ]
|
95
|
+
body! :RqBodyComp => [ :form ]
|
96
|
+
resp :RespComp => [ 'bad request', :json ]
|
97
|
+
end
|
98
|
+
|
99
|
+
open_api_set %i[index show], 'common response' do
|
100
|
+
response '567', 'query result export', :pdf, type: File
|
101
|
+
end
|
102
|
+
|
103
|
+
open_api :index, '(SUMMARY) this api blah blah ...' do
|
104
|
+
this_api_is_invalid! 'this api is expired!'
|
105
|
+
desc 'Optional multiline or single-line Markdown-formatted description',
|
106
|
+
id: 'user id',
|
107
|
+
email_addr: 'email_addr\'s desc'
|
108
|
+
email = 'git@github.com'
|
109
|
+
|
110
|
+
query! :id, Integer, enum: 0..5, length: [1, 2], pattern: /^[0-9]$/, range: {gt:0, le:5}
|
111
|
+
query! :done, Boolean, must_be: false, default: true, desc: 'must be false'
|
112
|
+
query :email_addr, String, lth: :ge_3, default: email # is_a: :email
|
113
|
+
# form! 'form', type: { id!: Integer, name: String }
|
114
|
+
file :pdf, 'desc: the media type is application/pdf'
|
115
|
+
|
116
|
+
response :success, 'success response', :json, type: :Dog
|
117
|
+
|
118
|
+
security :ApiKeyAuth
|
119
|
+
end
|
120
|
+
|
121
|
+
open_api :show do
|
122
|
+
param_ref :PathCompId, :QueryCompUuid
|
123
|
+
response_ref '123' => :RespComp, '223' => :RespComp
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
#### \> Explanation
|
130
|
+
|
131
|
+
##### \>\> controller class methods ([code source](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl.rb))
|
132
|
+
|
133
|
+
- `apis_set` [Optional]
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
- `open_api_set` [Optional]
|
138
|
+
|
139
|
+
|
140
|
+
- `open_api`
|
141
|
+
|
142
|
+
##### \>\> DSL methods inside *open_api* and *open_api_set*'s block ([code source](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl_inside_block.rb)# ApiInfoObj )
|
143
|
+
|
144
|
+
These methods in the block describe the specified API(s): description, valid?,
|
145
|
+
parameters, request body, responses, securities, servers.
|
146
|
+
|
147
|
+
- `this_api_is_invalid!`, its aliases are:
|
148
|
+
- `this_api_is_expired!`
|
149
|
+
- `this_api_is_unused!`
|
150
|
+
- `this_api_is_under_repair!`
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
# method signature
|
154
|
+
this_api_is_invalid! explain = ''
|
155
|
+
# usage
|
156
|
+
this_api_is_invalid! 'this api is expired!'
|
157
|
+
```
|
158
|
+
|
159
|
+
- `desc`: description for current API and its inputs (parameters and request body)
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
# method signature
|
163
|
+
desc desc, inputs_descs = { }
|
164
|
+
# usage
|
165
|
+
desc 'current API\'s description',
|
166
|
+
id: 'user id',
|
167
|
+
email_addr: 'email_addr\'s desc'
|
168
|
+
```
|
169
|
+
|
170
|
+
You can of course describe the input in it's DSL method (like `query! :done` this line),
|
171
|
+
but that will make it long and ugly. We recommend that unite descriptions in this place.
|
172
|
+
|
173
|
+
- param family methods
|
174
|
+
- `param`
|
175
|
+
- `param_ref`
|
176
|
+
- `header`, `path`, `query`, `cookie`
|
177
|
+
- bang methods: `header!`, `path!`, `query!`, `cookie!`
|
178
|
+
|
179
|
+
|
180
|
+
- request_body family methods
|
181
|
+
- `request_body`
|
182
|
+
- `body_ref`
|
183
|
+
- `body` and bang `body!`
|
184
|
+
|
185
|
+
|
186
|
+
- response family methods
|
187
|
+
- `response` (`resp`)
|
188
|
+
- `response_ref`
|
189
|
+
- `default_response` (`dft_resp`)
|
190
|
+
- `error_response` (`other_response`, `oth_resp`, `error`, `err_resp`)
|
191
|
+
|
192
|
+
##### \>\> DSL methods inside apis_set'block ([code source](https://github.com/zhandao/zero-rails_openapi/blob/master/lib/open_api/dsl_inside_block.rb)# CtrlInfoObj )
|
193
|
+
|
194
|
+
These methods in the block describe the current controller,
|
195
|
+
or we say, these methods eventually produce reusable components.
|
196
|
+
|
197
|
+
So, the following methods are used as above, except that you need to
|
198
|
+
name the key of the component, and pass it as the first of argument list, like:
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
query! :QueryCompUuid, :product_uuid, Integer, ...
|
202
|
+
```
|
203
|
+
This writing is feasible but not recommended,
|
204
|
+
because component's key and parameter's name looks like the same level.
|
205
|
+
The recommended writing is:
|
206
|
+
|
207
|
+
```ruby
|
208
|
+
query! :QueryCompUuid => [:product_uuid, Integer, ...]
|
209
|
+
```
|
210
|
+
The DSL methods used to generate the components in this block are:
|
211
|
+
(explained above)
|
212
|
+
|
213
|
+
- param family methods (except `param_ref`)
|
214
|
+
- request_body family methods (except `body_ref`)
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
### Generate JSON Documentation File
|
219
|
+
|
220
|
+
|
221
|
+
### Use Swagger-UI(Very Beautiful Web UI) to show your Documentation
|
222
|
+
|
223
|
+
|
224
|
+
## Troubleshooting
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
## Development
|
230
|
+
|
231
|
+
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.
|
232
|
+
|
233
|
+
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).
|
234
|
+
|
235
|
+
## Contributing
|
236
|
+
|
237
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/zhandao/zero-rails_openapi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
238
|
+
|
239
|
+
## License
|
240
|
+
|
241
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
242
|
+
|
243
|
+
## Code of Conduct
|
244
|
+
|
245
|
+
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).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "open_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|