jsonapi-swagger/lib/generators/jsonapi/swagger/templates/swagger.rb.erb
2019-04-22 16:26:27 +08:00

164 lines
6.9 KiB
Plaintext

require 'swagger_helper'
RSpec.describe '<%= resouces_name %>', type: :request do
let(:include) {''} #see https://github.com/domaindrivendev/rswag/issues/188
before(:each) do
@<%= model_name %> = create :<%= model_name %>
end
path '/<%= route_resouces %>' do
get '<%= route_resouces %>' do
tags '<%= route_resouces %>'
produces 'application/vnd.api+json'
parameter name: :'page[number]', in: :query, type: :string, description: '页码', required: false
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
schema type: :object,
properties: {
data: {
type: :array,
items: {
type: :object,
properties: {
id: { type: :string, description: 'ID'},
links: {
type: :object,
properties: {
self: { type: :string, description: '详情链接'},
},
description: '详情链接'
},
attributes: {
type: :object,
properties: {
<% attributes.each_key.each do |attr| -%>
<%= 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 -%>
},
description: '属性'
},
relationships: {
type: :object,
properties: {
<% relationships.each do |relation_name, relation| -%>
<% relation_name_camelize = relation_name.to_s.camelize -%>
<%= relation_name %>: {
type: :object,
properties: {
links: {
type: :object,
properties: {
self: { type: :string, description: '关联<%= relation_name_camelize %>列表链接' },
related: { type: :string, description: '相关<%= relation_name_camelize %>链接' },
},
description: '相关的<%= relation_name_camelize %>链接'
},
},
description: '相关<%= relation_name_camelize %>'
},
<% end -%>
},
description: '关联数据'
}
},
},
description: '数据'
},
meta: {
type: :object,
properties: {
record_count: { type: :integer, description: '总记录数'},
page_count: { type: :integer, description: '页数'},
},
description: '分页记录数'
},
links: {
type: :object,
properties: {
first: { type: :string, description: '第一页'},
next: { type: :string, description: '下一页'},
last: { type: :string, description: '最后一页'},
},
description: '分页链接' },
},
required: [:data]
run_test!
end
end
end
path '/<%= route_resouces %>/{id}' do
get '<%= route_resouces %>详情' do
tags '<%= route_resouces %>'
produces 'application/vnd.api+json'
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
schema type: :object,
properties: {
data: {
type: :object,
properties: {
id: { type: :string, description: 'ID'},
type: { type: :string, description: 'Type'},
links: {
type: :object,
properties: {
self: { type: :string, description: '详情链接'},
},
description: '详情链接'
},
attributes: {
type: :object,
properties: {
<% attributes.each_key.each do |attr| -%>
<%= 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 -%>
},
description: '属性'
},
relationships: {
type: :object,
properties: {
<% relationships.each do |relation_name, relation| -%>
<% relation_name_camelize = relation_name.to_s.camelize -%>
<%= relation_name %>: {
type: :object,
properties: {
links: {
type: :object,
properties: {
self: { type: :string, description: '关联<%= relation_name_camelize %>列表链接' },
related: { type: :string, description: '相关<%= relation_name_camelize %>链接' },
},
description: '相关的<%= relation_name_camelize %>链接'
},
},
description: '相关<%= relation_name_camelize %>'
},
<% end -%>
},
description: '关联数据'
}
},
description: '数据'
},
},
required: [:data]
let(:id) { @<%= model_name %>.id }
run_test!
end
end
end
<% unless resource_klass.immutable -%>
<% end -%>
end