Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allow_methods_for_attributes config option #4

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions lib/generators/jsonapi/swagger/swagger_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions lib/generators/jsonapi/swagger/templates/swagger.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/jsonapi/swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down