Skip to content

Commit

Permalink
accepts string as entity
Browse files Browse the repository at this point in the history
- adds changelog entry
  • Loading branch information
LeFnord committed Oct 31, 2016
1 parent 7d405ff commit 57ac313
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Lint/UselessAssignment:
Metrics/AbcSize:
Max: 56

Metrics/BlockLength:
Max: 165

# Offense count: 3
# Configuration parameters: CountComments.
Metrics/ClassLength:
Expand Down Expand Up @@ -74,6 +77,9 @@ Style/FileName:
- 'spec/swagger_v2/api_swagger_v2_type-format_spec.rb'
- 'spec/swagger_v2/grape-swagger_spec.rb'

Style/FrozenStringLiteralComment:
Enabled: false

# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#### Fixes

* [#527](https://github.com/ruby-grape/grape-swagger/pull/527): Accepts string as entity - [@LeFnord](https://github.com/LeFnord).
* [#515](https://github.com/ruby-grape/grape-swagger/pull/515): Removes limit on model names - [@LeFnord](https://github.com/LeFnord).
* [#511](https://github.com/ruby-grape/grape-swagger/pull/511): Fix incorrect data type linking for request params of entity types - [@serggl](https://github.com/serggl).
* Your contribution here.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'http://rubygems.org'

gemspec

case version = ENV['GRAPE_VERSION'] || '~> 0.17.0'
case version = ENV['GRAPE_VERSION'] || '~> 0.18'
when 'HEAD'
gem 'grape', github: 'ruby-grape/grape'
else
Expand Down
2 changes: 1 addition & 1 deletion grape-swagger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bundler'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'rack-cors'
s.add_development_dependency 'rubocop', '0.40.0'
s.add_development_dependency 'rubocop' # , '0.40.0'
s.add_development_dependency 'kramdown'
s.add_development_dependency 'redcarpet' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
s.add_development_dependency 'rouge' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/doc_methods/optional_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def build(key, options, request = nil)
end

def evaluate(key, options, request)
options[key].arity == 0 ? options[key].call : options[key].call(request)
options[key].arity.zero? ? options[key].call : options[key].call(request)
end

def default_values
Expand Down
38 changes: 18 additions & 20 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call(param, settings, route, definitions)
# optional properties
document_description(settings)
document_type_and_format(data_type)
document_array_param(value_type, definitions)
document_array_param(value_type, definitions) if value_type[:is_array]
document_default_value(settings)
document_range_values(settings)
document_required(settings)
Expand Down Expand Up @@ -61,27 +61,25 @@ def document_type_and_format(data_type)
end

def document_array_param(value_type, definitions)
if value_type[:is_array]
if value_type[:documentation].present?
param_type = value_type[:documentation][:param_type]
doc_type = value_type[:documentation][:type]
type = GrapeSwagger::DocMethods::DataType.mapping(doc_type) if doc_type && !DataType.request_primitive?(doc_type)
collection_format = value_type[:documentation][:collectionFormat]
end

array_items = {}
if definitions[value_type[:data_type]]
array_items['$ref'] = "#/definitions/#{@parsed_param[:type]}"
else
array_items[:type] = type || @parsed_param[:type]
end
array_items[:format] = @parsed_param.delete(:format) if @parsed_param[:format]
if value_type[:documentation].present?
param_type = value_type[:documentation][:param_type]
doc_type = value_type[:documentation][:type]
type = GrapeSwagger::DocMethods::DataType.mapping(doc_type) if doc_type && !DataType.request_primitive?(doc_type)
collection_format = value_type[:documentation][:collectionFormat]
end

@parsed_param[:in] = param_type || 'formData'
@parsed_param[:items] = array_items
@parsed_param[:type] = 'array'
@parsed_param[:collectionFormat] = collection_format if %w(csv ssv tsv pipes multi).include?(collection_format)
array_items = {}
if definitions[value_type[:data_type]]
array_items['$ref'] = "#/definitions/#{@parsed_param[:type]}"
else
array_items[:type] = type || @parsed_param[:type]
end
array_items[:format] = @parsed_param.delete(:format) if @parsed_param[:format]

@parsed_param[:in] = param_type || 'formData'
@parsed_param[:items] = array_items
@parsed_param[:type] = 'array'
@parsed_param[:collectionFormat] = collection_format if %w(csv ssv tsv pipes multi).include?(collection_format)
end

def param_type(value_type)
Expand Down
3 changes: 1 addition & 2 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

require 'active_support'
require 'active_support/core_ext/string/inflections.rb'

Expand Down Expand Up @@ -279,6 +277,7 @@ def expose_params(value)
end

def expose_params_from_model(model)
model = model.is_a?(String) ? model.constantize : model
model_name = model_name(model)

return model_name if @definitions.key?(model_name)
Expand Down
39 changes: 39 additions & 0 deletions spec/issues/427_entity_as_string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'
require 'grape-entity'
require 'grape-swagger-entity'

describe '#427 nested entity given as string' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_427 do
module Permission
class WithoutRole < Grape::Entity
expose :id
expose :description
end
end

class RoleEntity < Grape::Entity
expose :id
expose :description
expose :role
expose :permissions, using: 'Permission::WithoutRole'
end
desc 'Get a list of roles',
success: RoleEntity
get '/' do
present [], with: RoleEntity
end
end

add_swagger_documentation format: :json
end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)['definitions']
end

specify { expect(subject.keys).to include 'RoleEntity', 'WithoutRole' }
end
2 changes: 1 addition & 1 deletion spec/support/model_parsers/entity_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,5 @@ class DocumentedHashAndArrayModel < Grape::Entity
end

def mounted_paths
%w( /thing /other_thing /dummy )
%w(/thing /other_thing /dummy)
end
2 changes: 1 addition & 1 deletion spec/support/model_parsers/mock_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,5 @@ class ApiResponse < OpenStruct; end
end

def mounted_paths
%w( /thing /other_thing /dummy )
%w(/thing /other_thing /dummy)
end
2 changes: 1 addition & 1 deletion spec/support/model_parsers/representable_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,5 +408,5 @@ class DocumentedHashAndArrayModel < Representable::Decorator
end

def mounted_paths
%w( /thing /other_thing /dummy )
%w(/thing /other_thing /dummy)
end
2 changes: 0 additions & 2 deletions spec/swagger_v2/api_swagger_v2_detail_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: UTF-8

require 'spec_helper'

def details
Expand Down

0 comments on commit 57ac313

Please sign in to comment.