mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-22 22:06:50 +00:00
update swagger generator and template
This commit is contained in:
parent
46d3c3b366
commit
c38bf936df
@ -18,12 +18,16 @@ module Jsonapi
|
|||||||
"#{file_name.downcase.pluralize}_spec.rb"
|
"#{file_name.downcase.pluralize}_spec.rb"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def model_name
|
||||||
|
file_name.downcase.singularize
|
||||||
|
end
|
||||||
|
|
||||||
def resouces_name
|
def resouces_name
|
||||||
model_class_name.pluralize
|
model_class_name.pluralize
|
||||||
end
|
end
|
||||||
|
|
||||||
def route_resouces
|
def route_resouces
|
||||||
resouces_name.downcase.gsub('::', '/')
|
resouces_name.tableize
|
||||||
end
|
end
|
||||||
|
|
||||||
def model_class_name
|
def model_class_name
|
||||||
@ -49,14 +53,16 @@ module Jsonapi
|
|||||||
def columns_with_comment
|
def columns_with_comment
|
||||||
@columns_with_comment ||= {}.tap do |clos|
|
@columns_with_comment ||= {}.tap do |clos|
|
||||||
model_klass.columns.each do |col|
|
model_klass.columns.each do |col|
|
||||||
clos[col.name.to_sym] = { type: swagger_type(col.type), comment: safe_encode(col.comment) }
|
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) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def swagger_type(type)
|
def swagger_type(column)
|
||||||
case type
|
return 'array' if column.array
|
||||||
|
case column.type
|
||||||
when :bigint, :integer then 'integer'
|
when :bigint, :integer then 'integer'
|
||||||
|
when :boolean then 'boolean'
|
||||||
else 'string'
|
else 'string'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,14 +1,20 @@
|
|||||||
require 'swagger_helper'
|
require 'swagger_helper'
|
||||||
RSpec.describe '<%= resouces_name %>', type: :request do
|
RSpec.describe '<%= resouces_name %>', type: :request do
|
||||||
<% if resource_klass.immutable -%>
|
let(:include) {''} #see https://github.com/domaindrivendev/rswag/issues/188
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
@<%= model_name %> = create :<%= model_name %>
|
||||||
|
end
|
||||||
|
|
||||||
path '/<%= route_resouces %>' do
|
path '/<%= route_resouces %>' do
|
||||||
get '<%= route_resouces %>' do
|
get '<%= route_resouces %>' do
|
||||||
tags '<%= route_resouces %>'
|
tags '<%= route_resouces %>'
|
||||||
produces 'application/vnd.api+json'
|
produces 'application/vnd.api+json'
|
||||||
parameter name: :'page[number]', in: :query, type: :string, description: '页码', required: false
|
parameter name: :'page[number]', in: :query, type: :string, description: '页码', required: false
|
||||||
parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false
|
parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false
|
||||||
<% relationships.keys.each do |relation| -%>
|
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '包含字段', required: false
|
||||||
parameter name: :'fields[<%= relation %>]', in: :query, type: :string, description: '包含字段', required: false
|
<% relationships.each_key do |relation| -%>
|
||||||
|
parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '包含字段', required: false
|
||||||
<% end -%>
|
<% end -%>
|
||||||
response '200', '获取列表' do
|
response '200', '获取列表' do
|
||||||
schema type: :object,
|
schema type: :object,
|
||||||
@ -18,7 +24,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
items: {
|
items: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: :integer, description: 'ID'},
|
id: { type: :string, description: 'ID'},
|
||||||
links: {
|
links: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
@ -29,8 +35,8 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
attributes: {
|
attributes: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
<% attributes.keys.each do |attr| -%>
|
<% attributes.each_key.each do |attr| -%>
|
||||||
<%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
|
<%= 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] %>'},
|
||||||
<% end -%>
|
<% end -%>
|
||||||
},
|
},
|
||||||
description: '属性'
|
description: '属性'
|
||||||
@ -70,7 +76,14 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
},
|
},
|
||||||
description: '分页记录数'
|
description: '分页记录数'
|
||||||
},
|
},
|
||||||
links: { type: :array, items: { type: :string }, description: '分页链接' },
|
links: {
|
||||||
|
type: :object,
|
||||||
|
properties: {
|
||||||
|
first: { type: :string, description: '第一页'},
|
||||||
|
next: { type: :string, description: '下一页'},
|
||||||
|
last: { type: :string, description: '最后一页'},
|
||||||
|
},
|
||||||
|
description: '分页链接' },
|
||||||
},
|
},
|
||||||
required: [:data]
|
required: [:data]
|
||||||
run_test!
|
run_test!
|
||||||
@ -83,16 +96,19 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
tags '<%= route_resouces %>'
|
tags '<%= route_resouces %>'
|
||||||
produces 'application/vnd.api+json'
|
produces 'application/vnd.api+json'
|
||||||
parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
|
parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
|
||||||
|
parameter name: :include, in: :query, type: :string, description: '包含关联数据', required: false
|
||||||
|
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '包含字段', required: false
|
||||||
|
<% relationships.each_key do |relation| -%>
|
||||||
|
parameter name: :'fields[<%= relation.to_s.pluralize %>]', in: :query, type: :string, description: '包含字段', required: false
|
||||||
|
<% end -%>
|
||||||
response '200', '获取详情' do
|
response '200', '获取详情' do
|
||||||
schema type: :object,
|
schema type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
data: {
|
data: {
|
||||||
type: :array,
|
|
||||||
items: {
|
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: :integer, description: 'ID'},
|
id: { type: :string, description: 'ID'},
|
||||||
|
type: { type: :string, description: 'Type'},
|
||||||
links: {
|
links: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
@ -103,8 +119,8 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
attributes: {
|
attributes: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: {
|
properties: {
|
||||||
<% attributes.keys.each do |attr| -%>
|
<% attributes.each_key.each do |attr| -%>
|
||||||
<%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
|
<%= 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] %>'},
|
||||||
<% end -%>
|
<% end -%>
|
||||||
},
|
},
|
||||||
description: '属性'
|
description: '属性'
|
||||||
@ -129,22 +145,19 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
description: '相关<%= relation_name_camelize %>'
|
description: '相关<%= relation_name_camelize %>'
|
||||||
},
|
},
|
||||||
<% end -%>
|
<% end -%>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
description: '关联数据'
|
description: '关联数据'
|
||||||
}
|
}
|
||||||
},
|
|
||||||
},
|
},
|
||||||
description: '数据'
|
description: '数据'
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
required: [:data]
|
required: [:data]
|
||||||
|
let(:id) { @<%= model_name %>.id }
|
||||||
run_test!
|
run_test!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
<% else -%>
|
<% unless resource_klass.immutable -%>
|
||||||
<% end -%>
|
<% end -%>
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user