Skip to content

some more spec adaptions #358

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

Merged
merged 3 commits into from
Mar 26, 2016
Merged
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ env:
- GRAPE_VERSION=0.12.0
- GRAPE_VERSION=0.13.0
- GRAPE_VERSION=0.14.0
- GRAPE_VERSION=HEAD
# - GRAPE_VERSION=HEAD
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actual head introduces a breaking change to get the route informations
[update] see: replace-rack-mount-with-new-router

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

n.n.n / 2016-03-16
==================
[#358](https://github.com/ruby-grape/grape-swagger/pull/358)

- removes `allowMultiple` property from params
- adds `format` to definition property
- renames `defaultValue` to `default`


[#356](https://github.com/ruby-grape/grape-swagger/pull/356)

- adds `consumes` setting
Expand Down
35 changes: 27 additions & 8 deletions lib/grape-swagger/doc_methods/data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ def call(value)
raw_data_type = value[:type] if value.is_a?(Hash)
raw_data_type ||= 'string'
case raw_data_type.to_s
when 'Boolean', 'Date', 'Integer', 'String', 'Float', 'JSON', 'Array'
raw_data_type.to_s.downcase
when 'Hash'
'object'
when 'Rack::Multipart::UploadedFile'
'File'
when 'Rack::Multipart::UploadedFile', 'File'
'file'
when 'Virtus::Attribute::Boolean'
'boolean'
when 'Boolean', 'Date', 'Integer', 'String', 'Float'
raw_data_type.to_s.downcase
when 'BigDecimal'
'long'
when 'DateTime'
'double'
when 'DateTime', 'Time'
'dateTime'
when 'Numeric'
'double'
'long'
when 'Symbol'
'string'
else
Expand All @@ -37,6 +37,22 @@ def parse_entity_name(model)
entity_parts.join('::')
end
end

def request_primitive?(type)
request_primitives.include?(type.to_s.downcase)
end

def primitive?(type)
primitives.include?(type.to_s.downcase)
end

def request_primitives
primitives + %w(object string boolean file json array)
end

def primitives
PRIMITIVE_MAPPINGS.keys.map(&:downcase)
end
end

PRIMITIVE_MAPPINGS = {
Expand All @@ -46,7 +62,10 @@ def parse_entity_name(model)
'double' => %w(number double),
'byte' => %w(string byte),
'date' => %w(string date),
'dateTime' => %w(string date-time)
'dateTime' => %w(string date-time),
'binary' => %w(string binary),
'password' => %w(string password),
'email' => %w(string email)
}.freeze
end
end
Expand Down
42 changes: 19 additions & 23 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ def call(param, value, route)
path = route.route_path
method = route.route_method

additional_documentation = value.is_a?(Hash) ? value[:documentation] : nil
data_type = GrapeSwagger::DocMethods::DataType.call(value)

if additional_documentation && value.is_a?(Hash)
additional_documentation = value[:documentation]
if additional_documentation
value = additional_documentation.merge(value)
end

description = value.is_a?(Hash) ? value[:desc] || value[:description] : nil
required = value.is_a?(Hash) ? value[:required] : false
default_value = value.is_a?(Hash) ? value[:default] : nil
example = value.is_a?(Hash) ? value[:example] : nil
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
values = value.is_a?(Hash) ? value[:values] : nil
name = (value.is_a?(Hash) && value[:full_name]) || param
description = value[:desc] || value[:description] || nil
required = value[:required] || false
default_value = value[:default] || nil
example = value[:example] || nil
is_array = value[:is_array] || false
values = value[:values] || nil
name = value[:full_name] || param
enum_or_range_values = parse_enum_or_range_values(values)

value_type = { value: value, data_type: data_type, path: path }
Expand All @@ -29,32 +28,29 @@ def call(param, value, route)
in: param_type(value_type, param, method, is_array),
name: name,
description: description,
type: data_type,
required: required,
allowMultiple: is_array
required: required
}

if GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS.key?(data_type)
parsed_params[:type], parsed_params[:format] = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type]
if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
data = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type]
parsed_params[:type], parsed_params[:format] = data
else
parsed_params[:type] = data_type
end

parsed_params[:items] = @array_items if @array_items.present?

parsed_params[:defaultValue] = example if example
parsed_params[:defaultValue] = default_value if default_value && example.blank?
parsed_params[:default] = example if example
parsed_params[:default] = default_value if default_value && example.blank?

parsed_params.merge!(enum_or_range_values) if enum_or_range_values
parsed_params
end

def primitive?(type)
%w(object integer long float double string byte boolean date datetime).include? type.to_s.downcase
end

private

def param_type(value_type, param, method, is_array)
# TODO: use `value_type.dig():value, :documentation, :param_type)` instead req ruby2.3
# TODO: use `value_type.dig():value, :documentation, :param_type)` instead, req ruby2.3
#
if value_type[:value].is_a?(Hash) &&
value_type[:value].key?(:documentation) &&
Expand All @@ -70,7 +66,7 @@ def param_type(value_type, param, method, is_array)
when value_type[:path].include?("{#{param}}")
'path'
when %w(POST PUT PATCH).include?(method)
primitive?(value_type[:data_type]) ? 'formData' : 'body'
GrapeSwagger::DocMethods::DataType.request_primitive?(value_type[:data_type]) ? 'formData' : 'body'
else
'query'
end
Expand Down
23 changes: 11 additions & 12 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@

module Grape
class Endpoint
PRIMITIVE_MAPPINGS = {
'integer' => %w(integer int32),
'long' => %w(integer int64),
'float' => %w(number float),
'double' => %w(number double),
'byte' => %w(string byte),
'date' => %w(string date),
'dateTime' => %w(string date-time)
}.freeze

def content_types_for(target_class)
content_types = (target_class.content_types || {}).values

Expand Down Expand Up @@ -248,7 +238,16 @@ def parse_response_params(params)
{ '$ref' => "#/definitions/#{name}" }
end
else
memo[x.first] = { type: GrapeSwagger::DocMethods::DataType.call(x.last[:documentation] || x.last) }

data_type = GrapeSwagger::DocMethods::DataType.call(x.last[:documentation] || x.last)

if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
data = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type]
memo[x.first] = { type: data.first, format: data.last }
else
memo[x.first] = { type: data_type }
end

memo[x.first][:enum] = x.last[:values] if x.last[:values] && x.last[:values].is_a?(Array)
end
end
Expand Down Expand Up @@ -279,7 +278,7 @@ def could_it_be_a_model?(value)
) || (
value[:type] &&
value[:type].is_a?(Class) &&
!GrapeSwagger::DocMethods::ParseParams.primitive?(value[:type].name.downcase) &&
!GrapeSwagger::DocMethods::DataType.primitive?(value[:type].name.downcase) &&
!value[:type] == Array
)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/data_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
describe "Rack::Multipart::UploadedFile" do
let(:value) { { type: Rack::Multipart::UploadedFile } }

it { expect(subject).to eql 'File' }
it { expect(subject).to eql 'file' }
end

describe "Virtus::Attribute::Boolean" do
Expand All @@ -34,7 +34,7 @@
describe "BigDecimal" do
let(:value) { { type: BigDecimal } }

it { expect(subject).to eql 'long' }
it { expect(subject).to eql 'double' }
end

describe "DateTime" do
Expand All @@ -46,7 +46,7 @@
describe "Numeric" do
let(:value) { { type: Numeric } }

it { expect(subject).to eql 'double' }
it { expect(subject).to eql 'long' }
end

describe "Symbol" do
Expand Down
Loading