jsonapi-swagger 0.1.0 → 0.2.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: ea93978c9a08288cd98899e5d3503a270be4093954bb073689f0c026771dc7ad
4
- data.tar.gz: 0a1d89cb33b692d5fd460b03d5a7983f9b24bbdf92a6563c1cd165eb2d47e825
3
+ metadata.gz: 8cfab1114fff50d85a5e1a794431587a8f6275e2dbcd2343b4ae4a527ae379e1
4
+ data.tar.gz: 535abfa4b0960bcaa7f752d30d64f7ba255306bd5652cb522088137754146f65
5
5
  SHA512:
6
- metadata.gz: ebe4803bee8ad8f5f0b52797c9ebdad9f6a425d3b308c58cbcc119fe43da9d4ff1732e9e4c195db34f0c3eb9df83ee1d9349650132ecfe079bcd3cd4305283ad
7
- data.tar.gz: d845a4f4e6147dbe2c74e89802177834a67f9f1dd9d06e4c827659b639de98277a6d5757ad95b27ac43712bb89f64ea6c3c13b0a9bca29746fa20122ff0a45a1
6
+ metadata.gz: f1846b3909f1fe9948e5e213818df33f763c9c75be9d055ba96149d6bb1f346a4cc1566b5e5b5270d9bb6a1934c03f4411470d48ecf662f5314b8306b77666af
7
+ data.tar.gz: 204262395c367fab8a0eb6febed30c4420a7bc8191aa872977c087418bd4b0c3bed08739325681a275fa2ddf664eedfa4f51f3520035b0ab53bfc26a82f67181
@@ -18,12 +18,16 @@ module Jsonapi
18
18
  "#{file_name.downcase.pluralize}_spec.rb"
19
19
  end
20
20
 
21
+ def model_name
22
+ file_name.downcase.singularize
23
+ end
24
+
21
25
  def resouces_name
22
26
  model_class_name.pluralize
23
27
  end
24
28
 
25
29
  def route_resouces
26
- resouces_name.downcase.gsub('::', '/')
30
+ resouces_name.tableize
27
31
  end
28
32
 
29
33
  def model_class_name
@@ -49,14 +53,16 @@ module Jsonapi
49
53
  def columns_with_comment
50
54
  @columns_with_comment ||= {}.tap do |clos|
51
55
  model_klass.columns.each do |col|
52
- clos[col.name.to_sym] = { type: swagger_type(col.type), comment: safe_encode(col.comment) }
56
+ clos[col.name.to_sym] = { type: swagger_type(col), items_type: col.type, is_array: col.array, nullable: col.null, comment: safe_encode(col.comment) }
53
57
  end
54
58
  end
55
59
  end
56
60
 
57
- def swagger_type(type)
58
- case type
61
+ def swagger_type(column)
62
+ return 'array' if column.array
63
+ case column.type
59
64
  when :bigint, :integer then 'integer'
65
+ when :boolean then 'boolean'
60
66
  else 'string'
61
67
  end
62
68
  end
@@ -1,14 +1,20 @@
1
1
  require 'swagger_helper'
2
2
  RSpec.describe '<%= resouces_name %>', type: :request do
3
- <% if resource_klass.immutable -%>
3
+ let(:include) {''} #see https://github.com/domaindrivendev/rswag/issues/188
4
+
5
+ before(:each) do
6
+ @<%= model_name %> = create :<%= model_name %>
7
+ end
8
+
4
9
  path '/<%= route_resouces %>' do
5
10
  get '<%= route_resouces %>' do
6
11
  tags '<%= route_resouces %>'
7
12
  produces 'application/vnd.api+json'
8
13
  parameter name: :'page[number]', in: :query, type: :string, description: '页码', required: false
9
14
  parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false
10
- <% relationships.keys.each do |relation| -%>
11
- parameter name: :'fields[<%= relation %>]', in: :query, type: :string, description: '包含字段', required: false
15
+ parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '包含字段', required: false
16
+ <% relationships.each_key do |relation| -%>
17
+ parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '包含字段', required: false
12
18
  <% end -%>
13
19
  response '200', '获取列表' do
14
20
  schema type: :object,
@@ -18,7 +24,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
18
24
  items: {
19
25
  type: :object,
20
26
  properties: {
21
- id: { type: :integer, description: 'ID'},
27
+ id: { type: :string, description: 'ID'},
22
28
  links: {
23
29
  type: :object,
24
30
  properties: {
@@ -29,8 +35,8 @@ RSpec.describe '<%= resouces_name %>', type: :request do
29
35
  attributes: {
30
36
  type: :object,
31
37
  properties: {
32
- <% attributes.keys.each do |attr| -%>
33
- <%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
38
+ <% attributes.each_key.each do |attr| -%>
39
+ <%= 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] %>'},
34
40
  <% end -%>
35
41
  },
36
42
  description: '属性'
@@ -70,7 +76,14 @@ RSpec.describe '<%= resouces_name %>', type: :request do
70
76
  },
71
77
  description: '分页记录数'
72
78
  },
73
- links: { type: :array, items: { type: :string }, description: '分页链接' },
79
+ links: {
80
+ type: :object,
81
+ properties: {
82
+ first: { type: :string, description: '第一页'},
83
+ next: { type: :string, description: '下一页'},
84
+ last: { type: :string, description: '最后一页'},
85
+ },
86
+ description: '分页链接' },
74
87
  },
75
88
  required: [:data]
76
89
  run_test!
@@ -83,16 +96,19 @@ RSpec.describe '<%= resouces_name %>', type: :request do
83
96
  tags '<%= route_resouces %>'
84
97
  produces 'application/vnd.api+json'
85
98
  parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
86
-
99
+ parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false
100
+ parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '包含字段', required: false
101
+ <% relationships.each_key do |relation| -%>
102
+ parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '包含字段', required: false
103
+ <% end -%>
87
104
  response '200', '获取详情' do
88
105
  schema type: :object,
89
106
  properties: {
90
107
  data: {
91
- type: :array,
92
- items: {
93
108
  type: :object,
94
109
  properties: {
95
- id: { type: :integer, description: 'ID'},
110
+ id: { type: :string, description: 'ID'},
111
+ type: { type: :string, description: 'Type'},
96
112
  links: {
97
113
  type: :object,
98
114
  properties: {
@@ -103,8 +119,8 @@ RSpec.describe '<%= resouces_name %>', type: :request do
103
119
  attributes: {
104
120
  type: :object,
105
121
  properties: {
106
- <% attributes.keys.each do |attr| -%>
107
- <%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
122
+ <% attributes.each_key.each do |attr| -%>
123
+ <%= 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] %>'},
108
124
  <% end -%>
109
125
  },
110
126
  description: '属性'
@@ -129,22 +145,19 @@ RSpec.describe '<%= resouces_name %>', type: :request do
129
145
  description: '相关<%= relation_name_camelize %>'
130
146
  },
131
147
  <% end -%>
132
-
133
-
134
-
135
148
  },
136
149
  description: '关联数据'
137
150
  }
138
- },
139
151
  },
140
152
  description: '数据'
141
153
  },
142
154
  },
143
155
  required: [:data]
156
+ let(:id) { @<%= model_name %>.id }
144
157
  run_test!
145
158
  end
146
159
  end
147
160
  end
148
- <% else -%>
161
+ <% unless resource_klass.immutable -%>
149
162
  <% end -%>
150
163
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jsonapi
4
4
  module Swagger
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.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.1.0
4
+ version: 0.2.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-04-18 00:00:00.000000000 Z
11
+ date: 2019-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler