From 6bbaeea545d4539979eb192a5e1e808d465b1145 Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:00:00 +0300 Subject: [PATCH 01/11] Add allow_methods_for_attributes config option --- lib/generators/jsonapi/swagger/swagger_generator.rb | 4 ++++ lib/generators/jsonapi/swagger/templates/swagger.json.erb | 3 +++ lib/jsonapi/swagger.rb | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/swagger_generator.rb b/lib/generators/jsonapi/swagger/swagger_generator.rb index f8b53a5..32fde12 100644 --- a/lib/generators/jsonapi/swagger/swagger_generator.rb +++ b/lib/generators/jsonapi/swagger/swagger_generator.rb @@ -33,6 +33,10 @@ def json_file ) end + def swagger_allow_methods_for_attributes + Jsonapi::Swagger.allow_methods_for_attributes + end + def swagger_version Jsonapi::Swagger.version end diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 8c3a44c..94ae952 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -75,6 +75,9 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| + attribute_type = props.dig(attr, :type) + attribute_type ||= "<%= swagger_allow_methods_for_attributes %>" ? :string : raise("Type for #{attr} attribute is not found in the schema. Consider enabling allow_methods_for_attributes=true config option.") + attrs.each do |attr| columns = columns_with_comment(need_encoding: false) props[attr][:type] = columns[attr][:type] diff --git a/lib/jsonapi/swagger.rb b/lib/jsonapi/swagger.rb index 4084ce6..0533e1c 100644 --- a/lib/jsonapi/swagger.rb +++ b/lib/jsonapi/swagger.rb @@ -10,7 +10,11 @@ module Swagger class Error < StandardError; end class << self - attr_accessor :version, :info, :file_path, :base_path, :use_rswag + attr_accessor :allow_methods_for_attributes, :version, :info, :file_path, :base_path, :use_rswag + + def allow_methods_for_attributes + @allow_methods_for_attributes ||= true + end def config yield self From 0cb18d91b51f8ab2e37b3457f1b2a7a521780da2 Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:12:33 +0300 Subject: [PATCH 02/11] Fix --- .../jsonapi/swagger/templates/swagger.json.erb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 94ae952..c23fc05 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,11 +76,14 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attribute_type = props.dig(attr, :type) - attribute_type ||= "<%= swagger_allow_methods_for_attributes %>" ? :string : raise("Type for #{attr} attribute is not found in the schema. Consider enabling allow_methods_for_attributes=true config option.") - + attribute_type ||= if "<%= swagger_allow_methods_for_attributes %>" + :string + else + raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") + end attrs.each do |attr| columns = columns_with_comment(need_encoding: false) - props[attr][:type] = columns[attr][:type] + props[attr][:type] = attribute_type props[attr][:items] = { type: columns[attr][:items_type] } if columns[attr][:is_array] props[attr][:'x-nullable'] = columns[attr][:nullable] props[attr][:description] = columns[attr][:comment] From b0263f3a4934b2f03698ee3e59c26f5d984e61fc Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:17:42 +0300 Subject: [PATCH 03/11] Fix --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index c23fc05..c6a28a0 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,7 +76,7 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attribute_type = props.dig(attr, :type) - attribute_type ||= if "<%= swagger_allow_methods_for_attributes %>" + attribute_type ||= if <%= swagger_allow_methods_for_attributes %> :string else raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") From cf82e3340dd19c4f7e778a6d068d05245cc718c9 Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:34:34 +0300 Subject: [PATCH 04/11] Fix --- .../swagger/templates/swagger.json.erb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index c6a28a0..5ac347f 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -75,18 +75,17 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| - attribute_type = props.dig(attr, :type) - attribute_type ||= if <%= swagger_allow_methods_for_attributes %> - :string - else - raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") + attribute_props = props.fetch(attr) do + <%= swagger_allow_methods_for_attributes %> ? { type: :string, comment: false, read_only: true, nullable: true, }) : raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") end + attrs.each do |attr| - columns = columns_with_comment(need_encoding: false) - props[attr][:type] = attribute_type - props[attr][:items] = { type: columns[attr][:items_type] } if columns[attr][:is_array] - props[attr][:'x-nullable'] = columns[attr][:nullable] - props[attr][:description] = columns[attr][:comment] + columns = columns_with_comment(need_encoding: false) + props[attr][:type] = attribute_props[:type] + props[attr][:items] = { type: attribute_props[:items_type] } if attribute_props[:is_array] + props[attr][:'x-nullable'] = attribute_props[:nullable] + props[attr][:description] = attribute_props[:comment] + props[attr][:readOnly] = true if attribute_props[:read_only] end end end From 8389ef9ffd3c7a725e77a2789554c6c6cfccb36b Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:35:40 +0300 Subject: [PATCH 05/11] Fix --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 5ac347f..8bf6e1a 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,7 +76,7 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attribute_props = props.fetch(attr) do - <%= swagger_allow_methods_for_attributes %> ? { type: :string, comment: false, read_only: true, nullable: true, }) : raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") + swagger_allow_methods_for_attributes ? { type: :string, comment: false, read_only: true, nullable: true, }) : raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") end attrs.each do |attr| From a196b9eeac77d2b17ba5614224127898ce1b5b2b Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:38:53 +0300 Subject: [PATCH 06/11] Refactor properties --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 8bf6e1a..6f7231e 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,7 +76,8 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attribute_props = props.fetch(attr) do - swagger_allow_methods_for_attributes ? { type: :string, comment: false, read_only: true, nullable: true, }) : raise("Type for #{attr} attribute is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") + raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes + { type: :string, comment: false, read_only: true, nullable: true, }) end attrs.each do |attr| From e4120a6a288630f306dbb233c99e6896c56331d6 Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:43:34 +0300 Subject: [PATCH 07/11] Fix --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 6f7231e..0493fd1 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -77,7 +77,7 @@ Hash.new{|h, k| h[k] = {}} .tap do |props| attribute_props = props.fetch(attr) do raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes - { type: :string, comment: false, read_only: true, nullable: true, }) + { type: :string, comment: false, read_only: true, nullable: true, } end attrs.each do |attr| From 25d1f203621417058ca07f7e5ec2fa83103a9708 Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 11:48:30 +0300 Subject: [PATCH 08/11] Fix --- .../jsonapi/swagger/templates/swagger.json.erb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 0493fd1..cf21cfb 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -75,12 +75,12 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| - attribute_props = props.fetch(attr) do - raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes - { type: :string, comment: false, read_only: true, nullable: true, } - end - attrs.each do |attr| + attribute_props = props.fetch(attr) do + raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes + { type: :string, comment: false, read_only: true, nullable: true, } + end + columns = columns_with_comment(need_encoding: false) props[attr][:type] = attribute_props[:type] props[attr][:items] = { type: attribute_props[:items_type] } if attribute_props[:is_array] From 6c648c977a90564fe1788fdc9d2dafe851f8010c Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 12:06:09 +0300 Subject: [PATCH 09/11] Fix description --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index cf21cfb..6ae301f 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -78,7 +78,7 @@ attrs.each do |attr| attribute_props = props.fetch(attr) do raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes - { type: :string, comment: false, read_only: true, nullable: true, } + { type: :string, comment: nil, read_only: true, nullable: true, } end columns = columns_with_comment(need_encoding: false) From 32d07f1e552038124f6cd723e8c79bdc77d5c8fa Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 15:15:57 +0300 Subject: [PATCH 10/11] Fix properties --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index 6ae301f..c7183c7 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,7 +76,7 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attrs.each do |attr| - attribute_props = props.fetch(attr) do + attribute_props = columns.fetch(attr) do raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes { type: :string, comment: nil, read_only: true, nullable: true, } end From 2677bde34af32651b10d357f1792eb2d42983390 Mon Sep 17 00:00:00 2001 From: zergetaev Date: Wed, 31 Jul 2019 15:18:51 +0300 Subject: [PATCH 11/11] Fix properties --- lib/generators/jsonapi/swagger/templates/swagger.json.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/jsonapi/swagger/templates/swagger.json.erb b/lib/generators/jsonapi/swagger/templates/swagger.json.erb index c7183c7..d2b8088 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,12 +76,12 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attrs.each do |attr| + columns = columns_with_comment(need_encoding: false) attribute_props = columns.fetch(attr) do raise("`#{attr}` type is not found in the database schema. Consider setting `allow_methods_for_attributes` config option to `true`") unless swagger_allow_methods_for_attributes { type: :string, comment: nil, read_only: true, nullable: true, } end - columns = columns_with_comment(need_encoding: false) props[attr][:type] = attribute_props[:type] props[attr][:items] = { type: attribute_props[:items_type] } if attribute_props[:is_array] props[attr][:'x-nullable'] = attribute_props[:nullable]