mirror of
https://github.com/ditkrg/jsonapi-swagger.git
synced 2026-01-23 22:36:50 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 352fd3da35 | |||
| bee05fea2b | |||
| b301d53f02 | |||
| c7eaef79ab | |||
|
|
8efe5ff488 | ||
|
|
1b01e6a31a | ||
|
|
a0f7422793 | ||
|
|
9eae4f0b0b | ||
|
|
62ceb48307 | ||
|
|
5f7c3b03fa | ||
|
|
4c428d90b9 | ||
|
|
a4c974ab9e | ||
|
|
c24a3eec37 | ||
|
|
157735a266 | ||
|
|
b8e688165b | ||
|
|
f2e93dd395 | ||
|
|
e5fba11472 | ||
|
|
8e32f7dc88 |
32
.github/workflows/gempush.yml
vendored
Normal file
32
.github/workflows/gempush.yml
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
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.add_development_dependency 'bundler', '~> 2.0'
|
||||
spec.add_development_dependency 'rake', '~> 10.0'
|
||||
spec.add_development_dependency 'rake', '>= 12.3.3'
|
||||
spec.add_development_dependency 'rubocop', '~> 0.67'
|
||||
spec.add_development_dependency 'rswag', '~>2.0'
|
||||
end
|
||||
|
||||
@ -82,19 +82,19 @@ module Jsonapi
|
||||
end
|
||||
|
||||
def model_klass
|
||||
model_class_name.safe_constantize
|
||||
file_name.camelize.safe_constantize
|
||||
end
|
||||
|
||||
def resource_klass
|
||||
"#{model_class_name}Resource".safe_constantize
|
||||
@resource_klass ||= Jsonapi::Swagger::Resource.with(model_class_name)
|
||||
end
|
||||
|
||||
def attributes
|
||||
resource_klass._attributes.except(:id)
|
||||
resource_klass.attributes.except(:id)
|
||||
end
|
||||
|
||||
def relationships
|
||||
resource_klass._relationships
|
||||
resource_klass.relationships
|
||||
end
|
||||
|
||||
def sortable_fields
|
||||
@ -113,17 +113,34 @@ module Jsonapi
|
||||
resource_klass.filters
|
||||
end
|
||||
|
||||
def mutable?
|
||||
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
|
||||
is_array = col.respond_to?(:array) ? col.array : false
|
||||
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
|
||||
|
||||
def swagger_type(column)
|
||||
return 'array' if column.array
|
||||
return 'array' if column.respond_to?(:array) && column.array
|
||||
|
||||
case column.type
|
||||
when :bigint, :integer then 'integer'
|
||||
@ -132,6 +149,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)
|
||||
@ -140,7 +162,7 @@ module Jsonapi
|
||||
def tt(key, options={})
|
||||
options[:scope] = :jsonapi_swagger
|
||||
options[:default] = key.to_s.humanize
|
||||
I18n.t(key, options)
|
||||
I18n.t(key, **options)
|
||||
end
|
||||
|
||||
def 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
|
||||
@ -128,7 +128,7 @@
|
||||
},
|
||||
description: tt(:related_ids, model: relation_name_camelize)
|
||||
}
|
||||
if relation.belongs_to?
|
||||
if relation.try(:belongs_to?)
|
||||
relat_props[relation_name][:properties][:data] = {
|
||||
type: :object,
|
||||
properties: {
|
||||
@ -277,7 +277,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
if resource_klass.mutable?
|
||||
if mutable?
|
||||
doc['paths']["/#{route_resouces}"].merge!({
|
||||
post: {
|
||||
summary: "#{route_resouces} #{tt(:create)}",
|
||||
|
||||
@ -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,
|
||||
@ -173,7 +173,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
<% if resource_klass.mutable? -%>
|
||||
<% if mutable? -%>
|
||||
path '/<%= route_resouces %>' do
|
||||
post '<%= route_resouces %> <%= t(:create) %>' do
|
||||
tags '<%= route_resouces %>'
|
||||
@ -204,7 +204,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
|
||||
<%= relation_name %>: {
|
||||
type: :object,
|
||||
properties: {
|
||||
<% if relation.belongs_to? -%>
|
||||
<% if relation.try(:belongs_to?) -%>
|
||||
data: {
|
||||
type: :object,
|
||||
properties: {
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
require 'jsonapi/swagger/version'
|
||||
require 'jsonapi/swagger/railtie' if defined?(Rails)
|
||||
require 'jsonapi/swagger/json'
|
||||
require 'jsonapi/swagger/resource'
|
||||
|
||||
module Jsonapi
|
||||
module Swagger
|
||||
@ -34,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
|
||||
|
||||
15
lib/jsonapi/swagger/resource.rb
Normal file
15
lib/jsonapi/swagger/resource.rb
Normal file
@ -0,0 +1,15 @@
|
||||
require 'forwardable'
|
||||
module Jsonapi
|
||||
module Swagger
|
||||
class Resource
|
||||
def self.with(model_class_name)
|
||||
@resource_class = 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)
|
||||
end
|
||||
end
|
||||
end
|
||||
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 :@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
|
||||
18
lib/jsonapi/swagger/resources/jsonapi_resource.rb
Normal file
18
lib/jsonapi/swagger/resources/jsonapi_resource.rb
Normal file
@ -0,0 +1,18 @@
|
||||
require 'forwardable'
|
||||
module Jsonapi
|
||||
module Swagger
|
||||
class JsonapiResource
|
||||
extend Forwardable
|
||||
|
||||
def_delegators :@jr, :_attributes, :_relationships, :sortable_fields,
|
||||
:creatable_fields, :updatable_fields, :filters, :mutable?
|
||||
|
||||
def initialize(jr)
|
||||
@jr = jr
|
||||
end
|
||||
|
||||
alias attributes _attributes
|
||||
alias relationships _relationships
|
||||
end
|
||||
end
|
||||
end
|
||||
45
lib/jsonapi/swagger/resources/serializable_resource.rb
Normal file
45
lib/jsonapi/swagger/resources/serializable_resource.rb
Normal file
@ -0,0 +1,45 @@
|
||||
require 'forwardable'
|
||||
module Jsonapi
|
||||
module Swagger
|
||||
class SerializableResource
|
||||
extend Forwardable
|
||||
|
||||
def_delegators :@sr, :type_val, :attribute_blocks, :relationship_blocks, :link_blocks
|
||||
|
||||
def initialize(sr)
|
||||
@sr = sr
|
||||
end
|
||||
|
||||
alias attributes attribute_blocks
|
||||
|
||||
def relationships
|
||||
{}.tap do |relations|
|
||||
relationship_blocks.each do |rel, block|
|
||||
relations[rel] = OpenStruct.new(class_name: rel.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: from jsonapi serializable 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 Swagger
|
||||
VERSION = '0.6.0'
|
||||
VERSION = '0.8.3'
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user