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..d2b8088 100644 --- a/lib/generators/jsonapi/swagger/templates/swagger.json.erb +++ b/lib/generators/jsonapi/swagger/templates/swagger.json.erb @@ -76,11 +76,17 @@ def properties(attrs: []) Hash.new{|h, k| h[k] = {}} .tap do |props| attrs.each do |attr| - columns = columns_with_comment(need_encoding: false) - props[attr][:type] = columns[attr][: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) + 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 + + 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 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