diff --git a/lib/generators/jsonapi/swagger/swagger_generator.rb b/lib/generators/jsonapi/swagger/swagger_generator.rb index ddc9e4a..62bab17 100644 --- a/lib/generators/jsonapi/swagger/swagger_generator.rb +++ b/lib/generators/jsonapi/swagger/swagger_generator.rb @@ -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 @@ -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 diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 6793887..a153c2e 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -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 @@ -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 @@ -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) @@ -257,20 +257,20 @@ } 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 @@ -278,10 +278,10 @@ } 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], @@ -289,10 +289,10 @@ if mutable? } }) - 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, @@ -300,19 +300,19 @@ if mutable? } }) - 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'] ) %> diff --git a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb index 7305d8e..b917c64 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.rb.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.rb.erb @@ -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 @@ -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? -%> @@ -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 -%> @@ -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 -%> @@ -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, @@ -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: { @@ -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 @@ -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, @@ -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 diff --git a/lib/jsonapi/swagger/resource.rb b/lib/jsonapi/swagger/resource.rb index 2d31859..5f7c5c8 100644 --- a/lib/jsonapi/swagger/resource.rb +++ b/lib/jsonapi/swagger/resource.rb @@ -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 \ No newline at end of file +end