diff --git a/lib/generators/jsonapi/swagger/swagger_generator.rb b/lib/generators/jsonapi/swagger/swagger_generator.rb index eefa312..903e1e5 100644 --- a/lib/generators/jsonapi/swagger/swagger_generator.rb +++ b/lib/generators/jsonapi/swagger/swagger_generator.rb @@ -50,6 +50,10 @@ module Jsonapi resource_klass._relationships end + def filters + resource_klass.filters + end + def columns_with_comment @columns_with_comment ||= {}.tap do |clos| model_klass.columns.each do |col| @@ -60,6 +64,7 @@ module Jsonapi def swagger_type(column) return 'array' if column.array + case column.type when :bigint, :integer then 'integer' when :boolean then 'boolean' diff --git a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb index 3028211..106fa48 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb @@ -12,6 +12,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do produces 'application/vnd.api+json' parameter name: :'page[number]', in: :query, type: :string, description: '<%= t(:page_num) %>', required: false parameter name: :include, in: :query, type: :string, description: '<%= t(:include_related_data) %>', required: false +<% filters.each do |filter_attr, filter_config| -%> + parameter name: :'filter[<%= filter_attr %>]', in: :query, type: :string, description: '<%= t(:filter_field) %>', <% if filter_config[:default] -%>default: '<%= safe_encode(filter_config[:default]) %>',<% end %>required: false +<% end -%> parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% relationships.each_value do |relation| -%> parameter name: :'fields[<%= relation.class_name.tableize %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false @@ -36,7 +39,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do 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] %>'}, + <%= 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: '<%= t(:attributes) %>' @@ -120,7 +123,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do 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] %>'}, + <%= 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: '<%= t(:attributes) %>' diff --git a/lib/i18n/en.yml b/lib/i18n/en.yml index a446ba0..ffbc73f 100644 --- a/lib/i18n/en.yml +++ b/lib/i18n/en.yml @@ -3,6 +3,7 @@ en: page_num: 'Page Number' include_related_data: 'Include Related Data' display_field: 'Display Field' + filter_field: 'Filter Field' list: 'List' get_list: 'Fetch List' detail_link: 'Detail Link' diff --git a/lib/i18n/zh-CN.yml b/lib/i18n/zh-CN.yml index 9e9227a..36b9cd0 100644 --- a/lib/i18n/zh-CN.yml +++ b/lib/i18n/zh-CN.yml @@ -3,6 +3,7 @@ zh-CN: page_num: '页码' include_related_data: '包含关联数据' display_field: '显示字段' + filter_field: '过滤字段' list: '列表' get_list: '获取列表' detail_link: '详情链接'