jsonapi-swagger 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2396430a85aede1cfd926f160bde4437a848492306b4faaf2e153287140b5f1
4
- data.tar.gz: 9f09eb3b646ab27c3357d93c5b1ad542f8abb507186ac476b9ec17c98df10426
3
+ metadata.gz: 92c1984cded48de28c42fc2c6cea1942f17a985a485c0620941a55e8a6d8259c
4
+ data.tar.gz: 5ecc81a8c945a282346a0f20420be0cd047fcf715abea97986cf374285b780d1
5
5
  SHA512:
6
- metadata.gz: 13d71fa7dcc74a753a8f5ce78078e70031eb95b2bd9f10f71a1ea16c644c86fa8909b2766f65cdc9ff7e91a1432824983e70a5647e2956c3243c034f5b0f9c25
7
- data.tar.gz: 8063865a8234b71e50f0593f176c6b3e796f5c4a932022380649739ef677d62336b39a52bf0c90359381ae93b9f493a4ba4d97f66ee54409a1fa0c1f248c6d06
6
+ metadata.gz: b1918de62437786e214d29396a254693899d34325d3762a20c0327bf8f340b848a36a92ae3815e16b607fbb7b07957e7d2fc7ca3b842b9f991750edcc084f6cc
7
+ data.tar.gz: 8f93aa0c863bc241859ca952f2d103346552af40882bbfa35d68900561a832e0af3daf4c131c361e0bced923ccf1d35e3347828fe88565bb23663cc3805ebfde
data/README.md CHANGED
@@ -4,6 +4,8 @@ Generate JSONAPI Swagger Doc.
4
4
 
5
5
  [![Gem Version](https://img.shields.io/gem/v/jsonapi-swagger.svg)](https://rubygems.org/gems/jsonapi-swagger) [![GitHub license](https://img.shields.io/github/license/superiorlu/jsonapi-swagger.svg)](https://github.com/superiorlu/jsonapi-swagger/blob/master/LICENSE)
6
6
 
7
+ [![jsonapi-swagger-4-2-9.gif](https://i.loli.net/2019/05/05/5ccebf5e782b7.gif)](https://i.loli.net/2019/05/05/5ccebf5e782b7.gif)
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
@@ -34,6 +34,10 @@ module Jsonapi
34
34
  (class_path + [file_name]).map!(&:camelize).join("::")
35
35
  end
36
36
 
37
+ def sortable_fields_desc
38
+ t(:sortable_fields) + ': (-)' + sortable_fields.join(',')
39
+ end
40
+
37
41
  def model_klass
38
42
  model_class_name.safe_constantize
39
43
  end
@@ -50,6 +54,18 @@ module Jsonapi
50
54
  resource_klass._relationships
51
55
  end
52
56
 
57
+ def sortable_fields
58
+ resource_klass.sortable_fields
59
+ end
60
+
61
+ def creatable_fields
62
+ resource_klass.creatable_fields - relationships.keys
63
+ end
64
+
65
+ def updatable_fields
66
+ resource_klass.updatable_fields - relationships.keys
67
+ end
68
+
53
69
  def filters
54
70
  resource_klass.filters
55
71
  end
@@ -11,6 +11,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
11
11
  tags '<%= route_resouces %>'
12
12
  produces 'application/vnd.api+json'
13
13
  parameter name: :'page[number]', in: :query, type: :string, description: '<%= t(:page_num) %>', required: false
14
+ <% if sortable_fields.present? -%>
15
+ parameter name: :'sort', in: :query, type: :string, description: '<%= sortable_fields_desc %>', required: false
16
+ <% end -%>
14
17
  <% if relationships.present? -%>
15
18
  parameter name: :include, in: :query, type: :string, description: '<%= t(:include_related_data) %>', required: false
16
19
  <% end -%>
@@ -165,6 +168,264 @@ RSpec.describe '<%= resouces_name %>', type: :request do
165
168
  end
166
169
  end
167
170
  end
168
- <% unless resource_klass.immutable -%>
171
+
172
+ <% if resource_klass.mutable? -%>
173
+ path '/<%= route_resouces %>' do
174
+ post '<%= route_resouces %> <%= t(:create) %>' do
175
+ tags '<%= route_resouces %>'
176
+ consumes 'application/vnd.api+json'
177
+ produces 'application/vnd.api+json'
178
+ parameter name: :data,
179
+ in: :body,
180
+ type: :object,
181
+ properties: {
182
+ data: {
183
+ type: :object,
184
+ properties: {
185
+ type: { type: :string, default: '<%= route_resouces %>' },
186
+ attributes: {
187
+ type: :object,
188
+ properties: {
189
+ <% creatable_fields.each do |field| -%>
190
+ <%= field %>: { type: :<%= columns_with_comment[field][:type] %>, <%if columns_with_comment[field][:is_array] -%> items: { type: :<%= columns_with_comment[field][:items_type] %>},<% end -%>'x-nullable': <%= columns_with_comment[field][:nullable] %>, description: '<%= columns_with_comment[field][:comment] %>'},
191
+ <% end -%>
192
+ }
193
+ },
194
+ <% if relationships.present? -%>
195
+ relationships: {
196
+ type: :object,
197
+ properties: {
198
+ <% relationships.each do |relation_name, relation| -%>
199
+ <% relation_name_camelize = relation_name.to_s.camelize -%>
200
+ <%= relation_name %>: {
201
+ type: :object,
202
+ properties: {
203
+ <% if relation.belongs_to? -%>
204
+ data: {
205
+ type: :object,
206
+ properties: {
207
+ type: { type: :string, default: '<%= relation.table_name %>' },
208
+ id: { type: :string, description: '<%= relation_name_camelize %> ID' },
209
+ },
210
+ description: '<%= t(:related_id, model: relation_name_camelize) %>'
211
+ },
212
+ <% else -%>
213
+ data: {
214
+ type: :array,
215
+ items: {
216
+ type: :object,
217
+ properties: {
218
+ type: { type: :string, default: '<%= relation.table_name %>' },
219
+ id: { type: :string, description: '<%= relation_name_camelize %> ID' },
220
+ },
221
+ },
222
+ description: '<%= t(:related_ids, model: relation_name_camelize) %>'
223
+ },
224
+ <% end -%>
225
+ },
226
+ description: '<%= t(:related_ids, model: relation_name_camelize) %>'
227
+ },
228
+ <% end -%>
229
+ }
230
+ }
231
+ <% end -%>
232
+ }
233
+ },
234
+ },
235
+ description: '<%= t(:request_body) %>'
236
+
237
+ response '201', '<%= t(:create) %>' do
238
+ schema type: :object,
239
+ properties: {
240
+ data: {
241
+ type: :object,
242
+ properties: {
243
+ id: { type: :string, description: 'ID'},
244
+ type: { type: :string, description: 'Type'},
245
+ links: {
246
+ type: :object,
247
+ properties: {
248
+ self: { type: :string, description: '<%= t(:detail_link) %>'},
249
+ },
250
+ description: '<%= t(:detail_link) %>'
251
+ },
252
+ attributes: {
253
+ type: :object,
254
+ properties: {
255
+ <% attributes.each_key.each do |attr| -%>
256
+ <%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%>'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
257
+ <% end -%>
258
+ },
259
+ description: '<%= t(:attributes) %>'
260
+ },
261
+ relationships: {
262
+ type: :object,
263
+ properties: {
264
+ <% relationships.each do |relation_name, relation| -%>
265
+ <% relation_name_camelize = relation_name.to_s.camelize -%>
266
+ <%= relation_name %>: {
267
+ type: :object,
268
+ properties: {
269
+ links: {
270
+ type: :object,
271
+ properties: {
272
+ self: { type: :string, description: '<%= t(:associate_list_link, model: relation_name_camelize) %>' },
273
+ related: { type: :string, description: '<%= t(:related_link, model: relation_name_camelize )%>' },
274
+ },
275
+ description: '<%= t(:related_link, model: relation_name_camelize) %>'
276
+ },
277
+ },
278
+ description: '<%= t(:related_link, model: relation_name_camelize) %>'
279
+ },
280
+ <% end -%>
281
+ },
282
+ description: '<%= t(:associate_data) %>'
283
+ }
284
+ },
285
+ description: '<%= t(:data) %>'
286
+ },
287
+ },
288
+ required: [:data]
289
+ run_test!
290
+ end
291
+ end
292
+ end
293
+
294
+ path '/<%= route_resouces %>/{id}' do
295
+ patch '<%= route_resouces %> <%= t(:patch) %>' do
296
+ tags '<%= route_resouces %>'
297
+ consumes 'application/vnd.api+json'
298
+ produces 'application/vnd.api+json'
299
+ parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
300
+ parameter name: :data,
301
+ in: :body,
302
+ type: :object,
303
+ properties: {
304
+ data: {
305
+ type: :object,
306
+ properties: {
307
+ type: { type: :string, default: '<%= route_resouces %>' },
308
+ id: { type: :string },
309
+ attributes: {
310
+ type: :object,
311
+ properties: {
312
+ <% creatable_fields.each do |field| -%>
313
+ <%= field %>: { type: :<%= columns_with_comment[field][:type] %>, <%if columns_with_comment[field][:is_array] -%> items: { type: :<%= columns_with_comment[field][:items_type] %>},<% end -%>'x-nullable': <%= columns_with_comment[field][:nullable] %>, description: '<%= columns_with_comment[field][:comment] %>'},
314
+ <% end -%>
315
+ }
316
+ },
317
+ <% if relationships.present? -%>
318
+ relationships: {
319
+ type: :object,
320
+ properties: {
321
+ <% relationships.each do |relation_name, relation| -%>
322
+ <% relation_name_camelize = relation_name.to_s.camelize -%>
323
+ <%= relation_name %>: {
324
+ type: :object,
325
+ properties: {
326
+ <% if relation.belongs_to? -%>
327
+ data: {
328
+ type: :object,
329
+ properties: {
330
+ type: { type: :string, default: '<%= relation.table_name %>' },
331
+ id: { type: :string, description: '<%= relation_name_camelize %> ID' },
332
+ },
333
+ description: '<%= t(:related_id, model: relation_name_camelize) %>'
334
+ },
335
+ <% else -%>
336
+ data: {
337
+ type: :array,
338
+ items: {
339
+ type: :object,
340
+ properties: {
341
+ type: { type: :string, default: '<%= relation.table_name %>' },
342
+ id: { type: :string, description: '<%= relation_name_camelize %> ID' },
343
+ },
344
+ },
345
+ description: '<%= t(:related_ids, model: relation_name_camelize) %>'
346
+ },
347
+ <% end -%>
348
+ },
349
+ description: '<%= t(:related_ids, model: relation_name_camelize) %>'
350
+ },
351
+ <% end -%>
352
+ }
353
+ }
354
+ <% end -%>
355
+ }
356
+ },
357
+ },
358
+ description: '<%= t(:request_body) %>'
359
+
360
+ response '200', '<%= t(:patch) %>' do
361
+ schema type: :object,
362
+ properties: {
363
+ data: {
364
+ type: :object,
365
+ properties: {
366
+ id: { type: :string, description: 'ID'},
367
+ type: { type: :string, description: 'Type'},
368
+ links: {
369
+ type: :object,
370
+ properties: {
371
+ self: { type: :string, description: '<%= t(:detail_link) %>'},
372
+ },
373
+ description: '<%= t(:detail_link) %>'
374
+ },
375
+ attributes: {
376
+ type: :object,
377
+ properties: {
378
+ <% attributes.each_key.each do |attr| -%>
379
+ <%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%>'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
380
+ <% end -%>
381
+ },
382
+ description: '<%= t(:attributes) %>'
383
+ },
384
+ relationships: {
385
+ type: :object,
386
+ properties: {
387
+ <% relationships.each do |relation_name, relation| -%>
388
+ <% relation_name_camelize = relation_name.to_s.camelize -%>
389
+ <%= relation_name %>: {
390
+ type: :object,
391
+ properties: {
392
+ links: {
393
+ type: :object,
394
+ properties: {
395
+ self: { type: :string, description: '<%= t(:associate_list_link, model: relation_name_camelize) %>' },
396
+ related: { type: :string, description: '<%= t(:related_link, model: relation_name_camelize )%>' },
397
+ },
398
+ description: '<%= t(:related_link, model: relation_name_camelize) %>'
399
+ },
400
+ },
401
+ description: '<%= t(:related_link, model: relation_name_camelize) %>'
402
+ },
403
+ <% end -%>
404
+ },
405
+ description: '<%= t(:associate_data) %>'
406
+ }
407
+ },
408
+ description: '<%= t(:data) %>'
409
+ },
410
+ },
411
+ required: [:data]
412
+ let(:id) { @<%= model_name %>.id }
413
+ run_test!
414
+ end
415
+ end
416
+ end
417
+
418
+ path '/<%= route_resouces %>/{id}' do
419
+ delete '<%= route_resouces %> <%= t(:delete) %>' do
420
+ tags '<%= route_resouces %>'
421
+ produces 'application/vnd.api+json'
422
+ parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
423
+
424
+ response '204', '<%= t(:delete) %>' do
425
+ let(:id) { @<%= model_name %>.id }
426
+ run_test!
427
+ end
428
+ end
429
+ end
169
430
  <% end -%>
170
431
  end
data/lib/i18n/en.yml CHANGED
@@ -2,6 +2,7 @@ en:
2
2
  jsonapi_swagger:
3
3
  page_num: 'Page Number'
4
4
  include_related_data: 'Include Related Data'
5
+ sortable_fields: 'Sortable Fields'
5
6
  display_field: 'Display Field'
6
7
  filter_field: 'Filter Field'
7
8
  list: 'List'
@@ -22,4 +23,10 @@ en:
22
23
  page_links: 'Page Link'
23
24
  detail: 'Detail'
24
25
  get_dtail: 'Fetch Detail'
25
- detail_link: 'Detail Link'
26
+ detail_link: 'Detail Link'
27
+ create: 'Create'
28
+ patch: 'Patch'
29
+ delete: 'Delete'
30
+ related_id: 'Related %{model} ID'
31
+ related_ids: 'Related %{model} IDs'
32
+ request_body: 'Request Body'
data/lib/i18n/zh-CN.yml CHANGED
@@ -2,6 +2,7 @@ zh-CN:
2
2
  jsonapi_swagger:
3
3
  page_num: '页码'
4
4
  include_related_data: '包含关联数据'
5
+ sortable_fields: '排序字段'
5
6
  display_field: '显示字段'
6
7
  filter_field: '过滤字段'
7
8
  list: '列表'
@@ -22,4 +23,10 @@ zh-CN:
22
23
  page_links: '分页链接'
23
24
  detail: '详情'
24
25
  get_dtail: '获取详情'
25
- detail_link: '详情链接'
26
+ detail_link: '详情链接'
27
+ create: '创建'
28
+ patch: '更新'
29
+ delete: '删除'
30
+ related_id: '相关%{model}ID'
31
+ related_ids: '相关%{model}IDs'
32
+ request_body: '请求body'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jsonapi
4
4
  module Swagger
5
- VERSION = '0.4.1'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - YingRui Lu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-03 00:00:00.000000000 Z
11
+ date: 2019-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler