diff --git a/lib/generators/jsonapi/swagger/swagger_generator.rb b/lib/generators/jsonapi/swagger/swagger_generator.rb index f8b53a5..4e21d71 100644 --- a/lib/generators/jsonapi/swagger/swagger_generator.rb +++ b/lib/generators/jsonapi/swagger/swagger_generator.rb @@ -117,11 +117,23 @@ module Jsonapi resource_klass.mutable? end + def attribute_default + Jsonapi::Swagger.attribute_default + end + + def transform_method + @transform_method ||= resource_klass.transform_method if resource_klass.respond_to?(:transform_method) + end + def columns_with_comment(need_encoding: true) @columns_with_comment ||= {}.tap do |clos| + clos.default_proc = proc do |h, k| + h[k] = attribute_default + end model_klass.columns.each do |col| - clos[col.name.to_sym] = { type: swagger_type(col), items_type: col.type, is_array: col.array, nullable: col.null, comment: col.comment } - clos[col.name.to_sym][:comment] = safe_encode(col.comment) if need_encoding + col_name = transform_method ? col.name.send(transform_method) : col.name + clos[col_name.to_sym] = { type: swagger_type(col), items_type: col.type, is_array: col.array, nullable: col.null, comment: col.comment } + clos[col_name.to_sym][:comment] = safe_encode(col.comment) if need_encoding end end end @@ -136,6 +148,11 @@ module Jsonapi end end + def relation_table_name(relation) + return relation.class_name.tableize if relation.respond_to?(:class_name) + return relation.name if relation.respond_to?(:name) + end + def t(key, options={}) content = tt(key, options) safe_encode(content) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 8c3a44c..6793887 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -18,7 +18,7 @@ end parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false } relationships.each_value do |relation| - parameters << { name: :"fields[#{relation.class_name.tableize}]", in: :query, type: :string, description: tt(:display_field), required: false } + parameters << { name: :"fields[#{relation_table_name(relation)}]", in: :query, type: :string, description: tt(:display_field), required: false } end end end @@ -31,7 +31,7 @@ end parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false } relationships.each_value do |relation| - parameters << { name: :"fields[#{relation.class_name.tableize}]", in: :query, type: :string, description: tt(:display_field), required: false } + parameters << { name: :"fields[#{relation_table_name(relation)}]", in: :query, type: :string, description: tt(:display_field), required: false } end end end diff --git a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb index 1099dae..7305d8e 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb @@ -26,7 +26,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do <% 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 + parameter name: :'fields[<%= relation_table_name(relation) %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% end -%> response '200', '<%= t(:get_list) %>' do schema type: :object, @@ -113,7 +113,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do <% 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 + parameter name: :'fields[<%= relation_table_name(relation) %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false <% end -%> response '200', '<%= t(:get_detail) %>' do schema type: :object, diff --git a/lib/jsonapi/swagger.rb b/lib/jsonapi/swagger.rb index 4084ce6..9c0c689 100644 --- a/lib/jsonapi/swagger.rb +++ b/lib/jsonapi/swagger.rb @@ -35,6 +35,10 @@ module Jsonapi def use_rswag @use_rswag ||= false end + + def attribute_default + @attribute_default ||= { type: :string, nullable: true, comment: nil } + end end end end diff --git a/lib/jsonapi/swagger/resource.rb b/lib/jsonapi/swagger/resource.rb index b7f54e7..2d31859 100644 --- a/lib/jsonapi/swagger/resource.rb +++ b/lib/jsonapi/swagger/resource.rb @@ -17,6 +17,13 @@ module Jsonapi end require 'jsonapi/swagger/resources/serializable_resource' return Jsonapi::Swagger::SerializableResource.new(@resource_class) + elsif Object.const_defined?("#{model_class_name}Serializer") + @resource_class = "#{model_class_name}Serializer".safe_constantize + unless @resource_class < FastJsonapi::ObjectSerializer + raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of FastJsonapi::ObjectSerializer!" + end + require 'jsonapi/swagger/resources/fast_jsonapi_resource' + return Jsonapi::Swagger::FastJsonapiResource.new(@resource_class) else raise Jsonapi::Swagger::Error, "#{model_class_name} not support!" end diff --git a/lib/jsonapi/swagger/resources/fast_jsonapi_resource.rb b/lib/jsonapi/swagger/resources/fast_jsonapi_resource.rb new file mode 100644 index 0000000..213b151 --- /dev/null +++ b/lib/jsonapi/swagger/resources/fast_jsonapi_resource.rb @@ -0,0 +1,39 @@ +require 'forwardable' +module Jsonapi + module Swagger + class FastJsonapiResource + extend Forwardable + + def_delegators :@jr, :attributes_to_serialize, :relationships_to_serialize, :sortable_fields, + :creatable_fields, :updatable_fields, :filters, :mutable?, :transform_method + + def initialize(jr) + @jr = jr + end + + alias attributes attributes_to_serialize + alias relationships relationships_to_serialize + + # TODO: fast_jsonapi resource + def sortable_fields + [] + end + + def creatable_fields + [] + end + + def updatable_fields + [] + end + + def filters + [] + end + + def mutable? + false + end + end + end +end \ No newline at end of file