mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-22 13:56:54 +00:00
support fast_jsonapi
This commit is contained in:
parent
b8e688165b
commit
a4c974ab9e
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
39
lib/jsonapi/swagger/resources/fast_jsonapi_resource.rb
Normal file
39
lib/jsonapi/swagger/resources/fast_jsonapi_resource.rb
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user