Skip to content

Commit 3c37bdb

Browse files
committed
Merge pull request ruby-grape#358 from LeFnord/master
some more spec adaptions
2 parents e160ea1 + 8d5e81a commit 3c37bdb

20 files changed

+321
-189
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ env:
1616
- GRAPE_VERSION=0.12.0
1717
- GRAPE_VERSION=0.13.0
1818
- GRAPE_VERSION=0.14.0
19-
- GRAPE_VERSION=HEAD
19+
# - GRAPE_VERSION=HEAD

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
n.n.n / 2016-03-16
33
==================
4+
[#358](https://github.com/ruby-grape/grape-swagger/pull/358)
5+
6+
- removes `allowMultiple` property from params
7+
- adds `format` to definition property
8+
- renames `defaultValue` to `default`
9+
10+
411
[#356](https://github.com/ruby-grape/grape-swagger/pull/356)
512

613
- adds `consumes` setting

lib/grape-swagger/doc_methods/data_type.rb

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ def call(value)
66
raw_data_type = value[:type] if value.is_a?(Hash)
77
raw_data_type ||= 'string'
88
case raw_data_type.to_s
9+
when 'Boolean', 'Date', 'Integer', 'String', 'Float', 'JSON', 'Array'
10+
raw_data_type.to_s.downcase
911
when 'Hash'
1012
'object'
11-
when 'Rack::Multipart::UploadedFile'
12-
'File'
13+
when 'Rack::Multipart::UploadedFile', 'File'
14+
'file'
1315
when 'Virtus::Attribute::Boolean'
1416
'boolean'
15-
when 'Boolean', 'Date', 'Integer', 'String', 'Float'
16-
raw_data_type.to_s.downcase
1717
when 'BigDecimal'
18-
'long'
19-
when 'DateTime'
18+
'double'
19+
when 'DateTime', 'Time'
2020
'dateTime'
2121
when 'Numeric'
22-
'double'
22+
'long'
2323
when 'Symbol'
2424
'string'
2525
else
@@ -37,6 +37,22 @@ def parse_entity_name(model)
3737
entity_parts.join('::')
3838
end
3939
end
40+
41+
def request_primitive?(type)
42+
request_primitives.include?(type.to_s.downcase)
43+
end
44+
45+
def primitive?(type)
46+
primitives.include?(type.to_s.downcase)
47+
end
48+
49+
def request_primitives
50+
primitives + %w(object string boolean file json array)
51+
end
52+
53+
def primitives
54+
PRIMITIVE_MAPPINGS.keys.map(&:downcase)
55+
end
4056
end
4157

4258
PRIMITIVE_MAPPINGS = {
@@ -46,7 +62,10 @@ def parse_entity_name(model)
4662
'double' => %w(number double),
4763
'byte' => %w(string byte),
4864
'date' => %w(string date),
49-
'dateTime' => %w(string date-time)
65+
'dateTime' => %w(string date-time),
66+
'binary' => %w(string binary),
67+
'password' => %w(string password),
68+
'email' => %w(string email)
5069
}.freeze
5170
end
5271
end

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ def call(param, value, route)
77
path = route.route_path
88
method = route.route_method
99

10-
additional_documentation = value.is_a?(Hash) ? value[:documentation] : nil
1110
data_type = GrapeSwagger::DocMethods::DataType.call(value)
12-
13-
if additional_documentation && value.is_a?(Hash)
11+
additional_documentation = value[:documentation]
12+
if additional_documentation
1413
value = additional_documentation.merge(value)
1514
end
1615

17-
description = value.is_a?(Hash) ? value[:desc] || value[:description] : nil
18-
required = value.is_a?(Hash) ? value[:required] : false
19-
default_value = value.is_a?(Hash) ? value[:default] : nil
20-
example = value.is_a?(Hash) ? value[:example] : nil
21-
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
22-
values = value.is_a?(Hash) ? value[:values] : nil
23-
name = (value.is_a?(Hash) && value[:full_name]) || param
16+
description = value[:desc] || value[:description] || nil
17+
required = value[:required] || false
18+
default_value = value[:default] || nil
19+
example = value[:example] || nil
20+
is_array = value[:is_array] || false
21+
values = value[:values] || nil
22+
name = value[:full_name] || param
2423
enum_or_range_values = parse_enum_or_range_values(values)
2524

2625
value_type = { value: value, data_type: data_type, path: path }
@@ -29,32 +28,29 @@ def call(param, value, route)
2928
in: param_type(value_type, param, method, is_array),
3029
name: name,
3130
description: description,
32-
type: data_type,
33-
required: required,
34-
allowMultiple: is_array
31+
required: required
3532
}
3633

37-
if GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS.key?(data_type)
38-
parsed_params[:type], parsed_params[:format] = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type]
34+
if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
35+
data = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type]
36+
parsed_params[:type], parsed_params[:format] = data
37+
else
38+
parsed_params[:type] = data_type
3939
end
4040

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

43-
parsed_params[:defaultValue] = example if example
44-
parsed_params[:defaultValue] = default_value if default_value && example.blank?
43+
parsed_params[:default] = example if example
44+
parsed_params[:default] = default_value if default_value && example.blank?
4545

4646
parsed_params.merge!(enum_or_range_values) if enum_or_range_values
4747
parsed_params
4848
end
4949

50-
def primitive?(type)
51-
%w(object integer long float double string byte boolean date datetime).include? type.to_s.downcase
52-
end
53-
5450
private
5551

5652
def param_type(value_type, param, method, is_array)
57-
# TODO: use `value_type.dig():value, :documentation, :param_type)` instead req ruby2.3
53+
# TODO: use `value_type.dig():value, :documentation, :param_type)` instead, req ruby2.3
5854
#
5955
if value_type[:value].is_a?(Hash) &&
6056
value_type[:value].key?(:documentation) &&
@@ -70,7 +66,7 @@ def param_type(value_type, param, method, is_array)
7066
when value_type[:path].include?("{#{param}}")
7167
'path'
7268
when %w(POST PUT PATCH).include?(method)
73-
primitive?(value_type[:data_type]) ? 'formData' : 'body'
69+
GrapeSwagger::DocMethods::DataType.request_primitive?(value_type[:data_type]) ? 'formData' : 'body'
7470
else
7571
'query'
7672
end

lib/grape-swagger/endpoint.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33

44
module Grape
55
class Endpoint
6-
PRIMITIVE_MAPPINGS = {
7-
'integer' => %w(integer int32),
8-
'long' => %w(integer int64),
9-
'float' => %w(number float),
10-
'double' => %w(number double),
11-
'byte' => %w(string byte),
12-
'date' => %w(string date),
13-
'dateTime' => %w(string date-time)
14-
}.freeze
15-
166
def content_types_for(target_class)
177
content_types = (target_class.content_types || {}).values
188

@@ -248,7 +238,16 @@ def parse_response_params(params)
248238
{ '$ref' => "#/definitions/#{name}" }
249239
end
250240
else
251-
memo[x.first] = { type: GrapeSwagger::DocMethods::DataType.call(x.last[:documentation] || x.last) }
241+
242+
data_type = GrapeSwagger::DocMethods::DataType.call(x.last[:documentation] || x.last)
243+
244+
if GrapeSwagger::DocMethods::DataType.primitive?(data_type)
245+
data = GrapeSwagger::DocMethods::DataType::PRIMITIVE_MAPPINGS[data_type]
246+
memo[x.first] = { type: data.first, format: data.last }
247+
else
248+
memo[x.first] = { type: data_type }
249+
end
250+
252251
memo[x.first][:enum] = x.last[:values] if x.last[:values] && x.last[:values].is_a?(Array)
253252
end
254253
end
@@ -279,7 +278,7 @@ def could_it_be_a_model?(value)
279278
) || (
280279
value[:type] &&
281280
value[:type].is_a?(Class) &&
282-
!GrapeSwagger::DocMethods::ParseParams.primitive?(value[:type].name.downcase) &&
281+
!GrapeSwagger::DocMethods::DataType.primitive?(value[:type].name.downcase) &&
283282
!value[:type] == Array
284283
)
285284
end

spec/lib/data_type_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
describe "Rack::Multipart::UploadedFile" do
2323
let(:value) { { type: Rack::Multipart::UploadedFile } }
2424

25-
it { expect(subject).to eql 'File' }
25+
it { expect(subject).to eql 'file' }
2626
end
2727

2828
describe "Virtus::Attribute::Boolean" do
@@ -34,7 +34,7 @@
3434
describe "BigDecimal" do
3535
let(:value) { { type: BigDecimal } }
3636

37-
it { expect(subject).to eql 'long' }
37+
it { expect(subject).to eql 'double' }
3838
end
3939

4040
describe "DateTime" do
@@ -46,7 +46,7 @@
4646
describe "Numeric" do
4747
let(:value) { { type: Numeric } }
4848

49-
it { expect(subject).to eql 'double' }
49+
it { expect(subject).to eql 'long' }
5050
end
5151

5252
describe "Symbol" do

0 commit comments

Comments
 (0)