mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-24 14:56:50 +00:00
Compare commits
No commits in common. "master" and "v0.7.0" have entirely different histories.
32
.github/workflows/gempush.yml
vendored
32
.github/workflows/gempush.yml
vendored
@ -1,32 +0,0 @@
|
|||||||
name: jsonapi-swagger
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build + Publish
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@master
|
|
||||||
- name: Set up Ruby 2.6
|
|
||||||
uses: actions/setup-ruby@v1
|
|
||||||
with:
|
|
||||||
version: 2.6.x
|
|
||||||
|
|
||||||
- name: Publish to RubyGems
|
|
||||||
run: |
|
|
||||||
mkdir -p $HOME/.gem
|
|
||||||
touch $HOME/.gem/credentials
|
|
||||||
chmod 0600 $HOME/.gem/credentials
|
|
||||||
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
|
||||||
gem build *.gemspec
|
|
||||||
gem push *.gem
|
|
||||||
env:
|
|
||||||
GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
|
|
||||||
@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|||||||
spec.licenses = ['MIT']
|
spec.licenses = ['MIT']
|
||||||
|
|
||||||
spec.add_development_dependency 'bundler', '~> 2.0'
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
||||||
spec.add_development_dependency 'rake', '>= 12.3.3'
|
spec.add_development_dependency 'rake', '~> 10.0'
|
||||||
spec.add_development_dependency 'rubocop', '~> 0.67'
|
spec.add_development_dependency 'rubocop', '~> 0.67'
|
||||||
spec.add_development_dependency 'rswag', '~>2.0'
|
spec.add_development_dependency 'rswag', '~>2.0'
|
||||||
end
|
end
|
||||||
|
|||||||
@ -82,7 +82,7 @@ module Jsonapi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def model_klass
|
def model_klass
|
||||||
file_name.camelize.safe_constantize
|
model_class_name.safe_constantize
|
||||||
end
|
end
|
||||||
|
|
||||||
def resource_klass
|
def resource_klass
|
||||||
@ -117,30 +117,17 @@ module Jsonapi
|
|||||||
resource_klass.mutable?
|
resource_klass.mutable?
|
||||||
end
|
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)
|
def columns_with_comment(need_encoding: true)
|
||||||
@columns_with_comment ||= {}.tap do |clos|
|
@columns_with_comment ||= {}.tap do |clos|
|
||||||
clos.default_proc = proc do |h, k|
|
|
||||||
h[k] = attribute_default
|
|
||||||
end
|
|
||||||
model_klass.columns.each do |col|
|
model_klass.columns.each do |col|
|
||||||
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 }
|
||||||
is_array = col.respond_to?(:array) ? col.array : false
|
clos[col.name.to_sym][:comment] = safe_encode(col.comment) if need_encoding
|
||||||
clos[col_name.to_sym] = { type: swagger_type(col), items_type: col.type, is_array: is_array, nullable: col.null, comment: col.comment }
|
|
||||||
clos[col_name.to_sym][:comment] = safe_encode(col.comment) if need_encoding
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def swagger_type(column)
|
def swagger_type(column)
|
||||||
return 'array' if column.respond_to?(:array) && column.array
|
return 'array' if column.array
|
||||||
|
|
||||||
case column.type
|
case column.type
|
||||||
when :bigint, :integer then 'integer'
|
when :bigint, :integer then 'integer'
|
||||||
@ -149,11 +136,6 @@ module Jsonapi
|
|||||||
end
|
end
|
||||||
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={})
|
def t(key, options={})
|
||||||
content = tt(key, options)
|
content = tt(key, options)
|
||||||
safe_encode(content)
|
safe_encode(content)
|
||||||
@ -162,7 +144,7 @@ module Jsonapi
|
|||||||
def tt(key, options={})
|
def tt(key, options={})
|
||||||
options[:scope] = :jsonapi_swagger
|
options[:scope] = :jsonapi_swagger
|
||||||
options[:default] = key.to_s.humanize
|
options[:default] = key.to_s.humanize
|
||||||
I18n.t(key, **options)
|
I18n.t(key, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def safe_encode(content)
|
def safe_encode(content)
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
end
|
end
|
||||||
parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
||||||
relationships.each_value do |relation|
|
relationships.each_value do |relation|
|
||||||
parameters << { name: :"fields[#{relation_table_name(relation)}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
parameters << { name: :"fields[#{relation.class_name.tableize}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -31,7 +31,7 @@
|
|||||||
end
|
end
|
||||||
parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
||||||
relationships.each_value do |relation|
|
relationships.each_value do |relation|
|
||||||
parameters << { name: :"fields[#{relation_table_name(relation)}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
parameters << { name: :"fields[#{relation.class_name.tableize}]", in: :query, type: :string, description: tt(:display_field), required: false }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -26,7 +26,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
<% end -%>
|
<% end -%>
|
||||||
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
||||||
<% relationships.each_value do |relation| -%>
|
<% relationships.each_value do |relation| -%>
|
||||||
parameter name: :'fields[<%= relation_table_name(relation) %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
parameter name: :'fields[<%= relation.class_name.tableize %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
||||||
<% end -%>
|
<% end -%>
|
||||||
response '200', '<%= t(:get_list) %>' do
|
response '200', '<%= t(:get_list) %>' do
|
||||||
schema type: :object,
|
schema type: :object,
|
||||||
@ -113,7 +113,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
|||||||
<% end -%>
|
<% end -%>
|
||||||
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
||||||
<% relationships.each_value do |relation| -%>
|
<% relationships.each_value do |relation| -%>
|
||||||
parameter name: :'fields[<%= relation_table_name(relation) %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
parameter name: :'fields[<%= relation.class_name.tableize %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
|
||||||
<% end -%>
|
<% end -%>
|
||||||
response '200', '<%= t(:get_detail) %>' do
|
response '200', '<%= t(:get_detail) %>' do
|
||||||
schema type: :object,
|
schema type: :object,
|
||||||
|
|||||||
@ -35,10 +35,6 @@ module Jsonapi
|
|||||||
def use_rswag
|
def use_rswag
|
||||||
@use_rswag ||= false
|
@use_rswag ||= false
|
||||||
end
|
end
|
||||||
|
|
||||||
def attribute_default
|
|
||||||
@attribute_default ||= { type: :string, nullable: true, comment: nil }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,12 +3,23 @@ module Jsonapi
|
|||||||
module Swagger
|
module Swagger
|
||||||
class Resource
|
class Resource
|
||||||
def self.with(model_class_name)
|
def self.with(model_class_name)
|
||||||
@resource_class = model_class_name.safe_constantize
|
if Object.const_defined?("#{model_class_name}Resource")
|
||||||
unless @resource_class < JSONAPI::Serializable::Resource
|
@resource_class = "#{model_class_name}Resource".safe_constantize
|
||||||
raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Serializable::Resource!"
|
unless @resource_class < JSONAPI::Resource
|
||||||
|
raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Resource!"
|
||||||
|
end
|
||||||
|
require 'jsonapi/swagger/resources/jsonapi_resource'
|
||||||
|
return Jsonapi::Swagger::JsonapiResource.new(@resource_class)
|
||||||
|
elsif Object.const_defined?("Serializable#{model_class_name}")
|
||||||
|
@resource_class = "Serializable#{model_class_name}".safe_constantize
|
||||||
|
unless @resource_class < JSONAPI::Serializable::Resource
|
||||||
|
raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Serializable::Resource!"
|
||||||
|
end
|
||||||
|
require 'jsonapi/swagger/resources/serializable_resource'
|
||||||
|
return Jsonapi::Swagger::SerializableResource.new(@resource_class)
|
||||||
|
else
|
||||||
|
raise Jsonapi::Swagger::Error, "#{model_class_name} not support!"
|
||||||
end
|
end
|
||||||
require 'jsonapi/swagger/resources/serializable_resource'
|
|
||||||
return Jsonapi::Swagger::SerializableResource.new(@resource_class)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,39 +0,0 @@
|
|||||||
require 'forwardable'
|
|
||||||
module Jsonapi
|
|
||||||
module Swagger
|
|
||||||
class FastJsonapiResource
|
|
||||||
extend Forwardable
|
|
||||||
|
|
||||||
def_delegators :@fr, :attributes_to_serialize, :relationships_to_serialize, :sortable_fields,
|
|
||||||
:creatable_fields, :updatable_fields, :filters, :mutable?, :transform_method
|
|
||||||
|
|
||||||
def initialize(fr)
|
|
||||||
@fr = fr
|
|
||||||
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
|
|
||||||
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
module Jsonapi
|
module Jsonapi
|
||||||
module Swagger
|
module Swagger
|
||||||
VERSION = '0.8.3'
|
VERSION = '0.7.0'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user