openapi-rails 0.3.0 → 0.3.1
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/README.md +181 -16
- data/lib/openapi/mongoid/crud_actions.rb +0 -16
- data/lib/openapi/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7de2269c948f0ee5b643d6508f87551181bec70c
|
4
|
+
data.tar.gz: 7b2f7b0279fa82108b3aafcaa7a52743991c5a90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 344fd7b1b01c77d22bda310dd0ccbcc9719260e5b09e7e5a27cebbc68819b2f3c9e101b3a333045cfbb9ff1e5923f6cb4e4dbb7f09160861cc2130a98e48d843
|
7
|
+
data.tar.gz: 658ff23bab20e35dbdfe7e128b014abf73818d8349192f3ded01539315e189ca8aaf798279f407fba5f294e70312d65629d7b6c8cb62c797d0ac13095bbb6bfd
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
<img src="http://www.kra.vc/_Media/swagger-logo.png" align="right" width="
|
1
|
+
<img src="http://www.kra.vc/_Media/swagger-logo.png" align="right" width="100px" />
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/openapi-rails)
|
2
4
|
|
3
5
|
# OpenAPI
|
4
6
|
|
@@ -6,13 +8,15 @@ CRUD interface for Rails models with OpenAPI ([Swagger](http://swagger.io/))
|
|
6
8
|
specification support and [Swagger UI](http://swagger.io/swagger-ui/)
|
7
9
|
integration.
|
8
10
|
|
11
|
+
**This gem supports only Mongoid, for other ORMs RPs are welcome.**
|
12
|
+
|
9
13
|
|
10
14
|
## Installation
|
11
15
|
|
12
16
|
Add to your `Gemfile`:
|
13
17
|
|
14
18
|
```ruby
|
15
|
-
gem 'openapi'
|
19
|
+
gem 'openapi-rails'
|
16
20
|
```
|
17
21
|
|
18
22
|
For new projects we recommend to run generator to create basic setup:
|
@@ -21,41 +25,202 @@ For new projects we recommend to run generator to create basic setup:
|
|
21
25
|
rake g openapi:config
|
22
26
|
```
|
23
27
|
|
28
|
+
If you have an existing project, please look through this readme file first to
|
29
|
+
get an idea of how it works.
|
30
|
+
|
31
|
+
Generator is creating configuration file `config/initializer/openapi.rb` with
|
32
|
+
default `/api` configuration and base controller `app/controllers/api/base_controlle.rb` that provides CRUD actions and specification builder.
|
33
|
+
|
24
34
|
|
25
35
|
## Usage
|
26
36
|
|
37
|
+
*Here projects API considered to be available at `/api`. Please check out
|
38
|
+
[Multiple APIs](#multiple-apis) section for other options.*
|
39
|
+
|
40
|
+
To add `CRUD` interface for the `Project` model create an empty controller that
|
41
|
+
inherits from `BaseController` at `app/controllers/api/projects_controller.rb`:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
module Api
|
45
|
+
class ProjectsController < BaseController
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Map controller with `crud` helper, mount specification and documentation in
|
51
|
+
`routes.rb`:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
Rails.application.routes.draw do
|
55
|
+
namespace :api do
|
56
|
+
crud :projects
|
57
|
+
# Mount OpenAPI specification for API
|
58
|
+
mount_openapi_specification name: :default
|
59
|
+
end
|
60
|
+
|
61
|
+
# Mount Swagger UI documentation for API
|
62
|
+
mount_openapi_documentation
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
Add `Api::ProjectsController` to `config/initializer/openapi.rb` controllers
|
67
|
+
arrays of the default configuration:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
Openapi.configure do |config|
|
71
|
+
config.apis = {
|
72
|
+
default: {
|
73
|
+
title: 'Default',
|
74
|
+
description: '',
|
75
|
+
version: '1.0',
|
76
|
+
base_path: '/api',
|
77
|
+
controllers: [Api::ProjectsController]
|
78
|
+
}
|
79
|
+
}
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
Restart develoment server and open [http://localhost:3000/openapi](http://localhost:3000/openapi) where API documentation is served.
|
84
|
+
|
85
|
+

|
86
|
+
|
87
|
+
Meta information about mapped actions and model fields is pulled automatically
|
88
|
+
on project initialization. Changes in `routes.rb` or models would require server
|
89
|
+
restart to be reflected in documentation.
|
27
90
|
|
28
|
-
|
91
|
+
In documentation interface for `create` and `update` actions request `Example
|
92
|
+
Value` includes only required fields. Other model fields should be added
|
93
|
+
manually.
|
29
94
|
|
30
95
|
|
31
|
-
##
|
96
|
+
## Supported Features
|
32
97
|
|
33
|
-
|
98
|
+
Following features are supported out of the box:
|
99
|
+
|
100
|
+
- pagination via [kaminari](https://github.com/amatsuda/kaminari)
|
101
|
+
- has_scope via [has_scope](https://github.com/plataformatec/has_scope)
|
102
|
+
- search via [mongoid-search](https://github.com/mauriciozaffari/mongoid_search)
|
103
|
+
- version via [mongoid-history](https://github.com/aq1018/mongoid-history)
|
104
|
+
- `CSV` format for `index` action, requires `.csv` format to be added to the
|
105
|
+
request url, e.g. `/api/posts.csv`.
|
34
106
|
|
35
107
|
|
36
108
|
## Customization
|
37
109
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
110
|
+
In the controller there is a way override default behaviour with helpers:
|
111
|
+
|
112
|
+
- `per_page` — set page size (default `50`) for `index` action
|
113
|
+
- `resource_class` — set model class manually
|
114
|
+
- `resource_params` — override default method that allows everything
|
42
115
|
|
43
116
|
|
44
117
|
## Custom Actions
|
45
118
|
|
46
|
-
|
47
|
-
|
119
|
+
Mapped custom actions (not CRUD) will add a log message on server start that
|
120
|
+
controller misses specification. As a result they are not added to
|
121
|
+
documentation.
|
122
|
+
|
123
|
+
Specification for custom methods should be added manually. Check out
|
124
|
+
[Swagger Blocks](https://github.com/fotinakis/swagger-blocks) gem or
|
125
|
+
[specification builder](https://github.com/slate-studio/openapi-rails/blob/master/lib/openapi/mongoid/spec_builder.rb) code for the reference.
|
126
|
+
|
127
|
+
Here is an example of custom method specification:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
module Api
|
131
|
+
class ProjectsController < BaseController
|
132
|
+
def custom_resource_action
|
133
|
+
# TODO: Method implemetation goes here.
|
134
|
+
end
|
135
|
+
|
136
|
+
swagger_path "/projects/{id}/custom_resource_action" do
|
137
|
+
operation :get do
|
138
|
+
key :tags, ["Projects"]
|
139
|
+
key :summary, 'Show extra details'
|
140
|
+
key :operationId, "showExtraProjectDetailsById"
|
141
|
+
key :produces, %w(application/json)
|
142
|
+
|
143
|
+
parameter do
|
144
|
+
key :name, :id
|
145
|
+
key :type, :string
|
146
|
+
key :in, :path
|
147
|
+
key :required, true
|
148
|
+
end
|
149
|
+
|
150
|
+
response 200 do
|
151
|
+
schema do
|
152
|
+
key :'$ref', "Project"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
48
160
|
|
49
161
|
|
50
162
|
## Multiple APIs
|
51
163
|
|
52
|
-
|
164
|
+
There is a clean way to provide multiple APIs or API versions.
|
165
|
+
|
166
|
+
Here is an example of setting up two API versions:
|
167
|
+
|
168
|
+
`config/routes.rb`:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
Rails.application.routes.draw do
|
172
|
+
namespace :api do
|
173
|
+
namespace :v1 do
|
174
|
+
crud :projects
|
175
|
+
mount_openapi_specification name: :v1
|
176
|
+
end
|
177
|
+
|
178
|
+
namespace :v2 do
|
179
|
+
crud :projects
|
180
|
+
mount_openapi_specification name: :v2
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
mount_openapi_documentation
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
`config/initializer/openapi.rb`:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
Openapi.configure do |config|
|
192
|
+
config.apis = {
|
193
|
+
v1: {
|
194
|
+
title: 'Version 1',
|
195
|
+
description: 'Legacy API version, please check out Version 2.',
|
196
|
+
version: '1.0',
|
197
|
+
base_path: '/api/v1',
|
198
|
+
controllers: [Api::V1::ProjectsController]
|
199
|
+
},
|
200
|
+
v2: {
|
201
|
+
title: 'Version 2',
|
202
|
+
description: 'Latest stable API version.',
|
203
|
+
version: '2.0',
|
204
|
+
base_path: '/api/v2',
|
205
|
+
controllers: [Api::V2::ProjectsController]
|
206
|
+
}
|
207
|
+
}
|
208
|
+
end
|
209
|
+
```
|
210
|
+
|
211
|
+
Controllers with custom logic would be placed at `app/controllers/api/v1` and
|
212
|
+
`app/controllers/api/v2` modules.
|
213
|
+
|
214
|
+

|
215
|
+
|
53
216
|
|
217
|
+
## Contributors
|
54
218
|
|
55
|
-
|
219
|
+
- [Alexander Kravets](http://www.kra.vc)
|
220
|
+
- [Denis Popov](https://github.com/DenisPopov15)
|
56
221
|
|
57
|
-
|
58
|
-
|
222
|
+
If you have any ideas or questions please feel free to reach out! PRs are
|
223
|
+
welcome, tests are on the roadmap.
|
59
224
|
|
60
225
|
|
61
|
-
[Slate Studio](https://www.slatestudio.com)
|
226
|
+
`OpenAPI Rails` gem is maintained and funded by [Slate Studio LLC](https://www.slatestudio.com).
|
@@ -206,22 +206,6 @@ module Openapi
|
|
206
206
|
end
|
207
207
|
|
208
208
|
class_methods do
|
209
|
-
def index_json_config(hash)
|
210
|
-
self.index_json_config = hash
|
211
|
-
end
|
212
|
-
|
213
|
-
def create_json_config(hash)
|
214
|
-
self.create_json_config = hash
|
215
|
-
end
|
216
|
-
|
217
|
-
def show_json_config(hash)
|
218
|
-
self.show_json_config = hash
|
219
|
-
end
|
220
|
-
|
221
|
-
def update_json_config(hash)
|
222
|
-
self.update_json_config = hash
|
223
|
-
end
|
224
|
-
|
225
209
|
def resource_class(name)
|
226
210
|
self.resource_class = name
|
227
211
|
end
|
data/lib/openapi/version.rb
CHANGED