Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/generators/jsonapi/swagger/swagger_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def model_name
file_name.downcase.singularize
end

def resouces_name
def resources_name
model_class_name.pluralize
end

def route_resouces
resouces_name.tableize
def route_resources
resources_name.tableize
end

def model_class_name
Expand All @@ -82,7 +82,7 @@ def ori_sortable_fields_desc
end

def model_klass
file_name.camelize.safe_constantize
model_class_name.safe_constantize
end

def resource_klass
Expand Down
42 changes: 21 additions & 21 deletions lib/generators/jsonapi/swagger/templates/swagger.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
filters.each do |filter_attr, filter_config|
parameters << { name: :"filter[#{filter_attr}]", in: :query, type: :string, description: tt(:filter_field), required: false}
end
parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false }
parameters << { name: :"fields[#{route_resources}]", in: :query, type: :string, description: tt(:display_field), required: false }
relationships.each_value do |relation|
parameters << { name: :"fields[#{relation_table_name(relation)}]", in: :query, type: :string, description: tt(:display_field), required: false }
end
Expand All @@ -29,7 +29,7 @@
if relationships.present?
parameters << { name: :include, in: :query, type: :string, description: tt(:include_related_data), required: false }
end
parameters << { name: :"fields[#{route_resouces}]", in: :query, type: :string, description: tt(:display_field), required: false }
parameters << { name: :"fields[#{route_resources}]", in: :query, type: :string, description: tt(:display_field), required: false }
relationships.each_value do |relation|
parameters << { name: :"fields[#{relation_table_name(relation)}]", in: :query, type: :string, description: tt(:display_field), required: false }
end
Expand All @@ -45,7 +45,7 @@
data: {
type: :object,
properties: {
type: { type: :string, default: route_resouces },
type: { type: :string, default: route_resources },
attributes: {
type: :object,
properties: properties(attrs: creatable_fields)
Expand Down Expand Up @@ -257,62 +257,62 @@
}
end

doc['paths']["/#{route_resouces}"] = {
doc['paths']["/#{route_resources}"] = {
get: {
summary: "#{route_resouces} #{tt(:list)}",
tags: [route_resouces],
summary: "#{route_resources} #{tt(:list)}",
tags: [route_resources],
produces: ['application/vnd.api+json'],
parameters: list_resource_parameters,
responses: list_resource_responses
}
}

doc['paths']["/#{route_resouces}/{id}"] = {
doc['paths']["/#{route_resources}/{id}"] = {
get: {
summary: "#{route_resouces} #{tt(:detail)}",
tags: [route_resouces],
summary: "#{route_resources} #{tt(:detail)}",
tags: [route_resources],
produces: ['application/vnd.api+json'],
parameters: show_resource_parameters,
responses: show_resource_responses
}
}

if mutable?
doc['paths']["/#{route_resouces}"].merge!({
doc['paths']["/#{route_resources}"].merge!({
post: {
summary: "#{route_resouces} #{tt(:create)}",
tags: [route_resouces],
summary: "#{route_resources} #{tt(:create)}",
tags: [route_resources],
consumes: ['application/vnd.api+json'],
produces: ['application/vnd.api+json'],
parameters: [create_resource_parameters],
responses: create_resource_responses
}
})

doc['paths']["/#{route_resouces}/{id}"].merge!({
doc['paths']["/#{route_resources}/{id}"].merge!({
patch: {
summary: "#{route_resouces} #{tt(:patch)}",
tags: [route_resouces],
summary: "#{route_resources} #{tt(:patch)}",
tags: [route_resources],
consumes: ['application/vnd.api+json'],
produces: ['application/vnd.api+json'],
parameters: patch_resource_parameters,
responses: show_resource_responses
}
})

doc['paths']["/#{route_resouces}/{id}"].merge!({
doc['paths']["/#{route_resources}/{id}"].merge!({
delete: {
summary: "#{route_resouces} #{tt(:delete)}",
tags: [route_resouces],
summary: "#{route_resources} #{tt(:delete)}",
tags: [route_resources],
produces: ['application/vnd.api+json'],
parameters: delete_resource_parameters,
responses: delete_resource_responses
}
})
else
doc['paths']["/#{route_resouces}"].delete(:post)
doc['paths']["/#{route_resouces}/{id}"].delete(:patch)
doc['paths']["/#{route_resouces}/{id}"].delete(:delete)
doc['paths']["/#{route_resources}"].delete(:post)
doc['paths']["/#{route_resources}/{id}"].delete(:patch)
doc['paths']["/#{route_resources}/{id}"].delete(:delete)
end
-%>
"paths": <%= JSON.pretty_generate(doc['paths'] ) %>
Expand Down
40 changes: 20 additions & 20 deletions lib/generators/jsonapi/swagger/templates/swagger.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'swagger_helper'
RSpec.describe '<%= resouces_name %>', type: :request do
RSpec.describe '<%= resources_name %>', type: :request do
let(:include) {''} #see https://github.com/domaindrivendev/rswag/issues/188

before(:each) do
Expand All @@ -10,9 +10,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
<% end -%>
end

path '/<%= route_resouces %>' do
get '<%= route_resouces %> <%= t(:list) %>' do
tags '<%= route_resouces %>'
path '/<%= route_resources %>' do
get '<%= route_resources %> <%= t(:list) %>' do
tags '<%= route_resources %>'
produces 'application/vnd.api+json'
parameter name: :'page[number]', in: :query, type: :string, description: '<%= t(:page_num) %>', required: false
<% if sortable_fields.present? -%>
Expand All @@ -24,7 +24,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
<% 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
parameter name: :'fields[<%= route_resources %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
<% relationships.each_value do |relation| -%>
parameter name: :'fields[<%= relation_table_name(relation) %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
<% end -%>
Expand Down Expand Up @@ -103,15 +103,15 @@ RSpec.describe '<%= resouces_name %>', type: :request do
end
end

path '/<%= route_resouces %>/{id}' do
get '<%= route_resouces %> <%= t(:detail) %>' do
tags '<%= route_resouces %>'
path '/<%= route_resources %>/{id}' do
get '<%= route_resources %> <%= t(:detail) %>' do
tags '<%= route_resources %>'
produces 'application/vnd.api+json'
parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
<% if relationships.present? -%>
parameter name: :include, in: :query, type: :string, description: '<%= t(:include_related_data) %>', required: false
<% end -%>
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
parameter name: :'fields[<%= route_resources %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
<% relationships.each_value do |relation| -%>
parameter name: :'fields[<%= relation_table_name(relation) %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
<% end -%>
Expand Down Expand Up @@ -174,9 +174,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
end

<% if mutable? -%>
path '/<%= route_resouces %>' do
post '<%= route_resouces %> <%= t(:create) %>' do
tags '<%= route_resouces %>'
path '/<%= route_resources %>' do
post '<%= route_resources %> <%= t(:create) %>' do
tags '<%= route_resources %>'
consumes 'application/vnd.api+json'
produces 'application/vnd.api+json'
parameter name: :data,
Expand All @@ -186,7 +186,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
data: {
type: :object,
properties: {
type: { type: :string, default: '<%= route_resouces %>' },
type: { type: :string, default: '<%= route_resources %>' },
attributes: {
type: :object,
properties: {
Expand Down Expand Up @@ -295,9 +295,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
end
end

path '/<%= route_resouces %>/{id}' do
patch '<%= route_resouces %> <%= t(:patch) %>' do
tags '<%= route_resouces %>'
path '/<%= route_resources %>/{id}' do
patch '<%= route_resources %> <%= t(:patch) %>' do
tags '<%= route_resources %>'
consumes 'application/vnd.api+json'
produces 'application/vnd.api+json'
parameter name: :id, in: :path, type: :integer, description: 'ID', required: true
Expand All @@ -308,7 +308,7 @@ path '/<%= route_resouces %>/{id}' do
data: {
type: :object,
properties: {
type: { type: :string, default: '<%= route_resouces %>' },
type: { type: :string, default: '<%= route_resources %>' },
id: { type: :string },
attributes: {
type: :object,
Expand Down Expand Up @@ -419,9 +419,9 @@ path '/<%= route_resouces %>/{id}' do
end
end

path '/<%= route_resouces %>/{id}' do
delete '<%= route_resouces %> <%= t(:delete) %>' do
tags '<%= route_resouces %>'
path '/<%= route_resources %>/{id}' do
delete '<%= route_resources %> <%= t(:delete) %>' do
tags '<%= route_resources %>'
produces 'application/vnd.api+json'
parameter name: :id, in: :path, type: :integer, description: 'ID', required: true

Expand Down
35 changes: 19 additions & 16 deletions lib/jsonapi/swagger/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,34 @@ module Jsonapi
module Swagger
class Resource
def self.with(model_class_name)
if Object.const_defined?("#{model_class_name}Resource")
@resource_class = "#{model_class_name}Resource".safe_constantize
unless @resource_class < JSONAPI::Resource
raise Jsonapi::Swagger::Error, "#{@resource_class.class} is not Subclass of JSONAPI::Resource!"
if (resource_class = "#{model_class_name}Resource".safe_constantize)
unless resource_class < JSONAPI::Resource
raise Jsonapi::Swagger::Error, "#{resource_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!"
return Jsonapi::Swagger::JsonapiResource.new(resource_class)
elsif (resource_class = serializable_class_name(model_class_name).safe_constantize)
unless resource_class < JSONAPI::Serializable::Resource
raise Jsonapi::Swagger::Error, "#{resource_class} is not Subclass of JSONAPI::Serializable::Resource!"
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!"
return Jsonapi::Swagger::SerializableResource.new(resource_class)
elsif (resource_class = "#{model_class_name}Serializer".safe_constantize)
unless resource_class < FastJsonapi::ObjectSerializer
raise Jsonapi::Swagger::Error, "#{resource_class} is not Subclass of FastJsonapi::ObjectSerializer!"
end
require 'jsonapi/swagger/resources/fast_jsonapi_resource'
return Jsonapi::Swagger::FastJsonapiResource.new(@resource_class)
return Jsonapi::Swagger::FastJsonapiResource.new(resource_class)
else
raise Jsonapi::Swagger::Error, "#{model_class_name} not support!"
end
end

def self.serializable_class_name(model_class_name)
parts = model_class_name.split('::')
parts[-1] = "Serializable#{parts[-1]}"
parts.join('::')
end
end
end
end
end
Loading