Skip to content

Commit 828b285

Browse files
committed
Merge pull request #234 from azhi/fix-param-values-float-range-error
Fixed the range :values options, now it does not fail with float range
2 parents daaad4c + 7b5573d commit 828b285

7 files changed

+163
-60
lines changed

.rubocop_todo.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-03-13 06:55:39 -0400 using RuboCop version 0.27.0.
2+
# on 2015-03-18 19:06:42 +0300 using RuboCop version 0.27.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
66
# versions of RuboCop, may require this file to be generated again.
77

8-
# Offense count: 10
8+
# Offense count: 11
99
Metrics/AbcSize:
10-
Max: 360
10+
Max: 356
1111

1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 496
15+
Max: 504
1616

1717
# Offense count: 6
1818
Metrics/CyclomaticComplexity:
19-
Max: 106
19+
Max: 102
2020

21-
# Offense count: 300
21+
# Offense count: 303
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
2424
Max: 254
2525

2626
# Offense count: 20
2727
# Configuration parameters: CountComments.
2828
Metrics/MethodLength:
29-
Max: 377
29+
Max: 376
3030

3131
# Offense count: 5
3232
Metrics/PerceivedComplexity:
33-
Max: 108
33+
Max: 104
3434

3535
# Offense count: 8
3636
Style/ClassVars:

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ env:
1414
- GRAPE_VERSION=0.9.0
1515
- GRAPE_VERSION=0.10.0
1616
- GRAPE_VERSION=0.10.1
17+
- GRAPE_VERSION=0.11.0
1718
- GRAPE_VERSION=HEAD

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#### Fixes
1010

1111
* [#232](https://github.com/tim-vandecasteele/grape-swagger/pull/232): Fixed missing raw array params - [@u2](https://github.com/u2).
12+
* [#234](https://github.com/tim-vandecasteele/grape-swagger/pull/234): Fixed range :values with float - [@azhi](https://github.com/azhi).
1213

1314
### 0.10.1 (March 11, 2015)
1415

lib/grape-swagger.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ def parse_array_params(params)
160160
modified_params
161161
end
162162

163+
def parse_enum_values(values)
164+
if values.is_a?(Range) && [Integer, String].any? { |klass| values.first.is_a?(klass) }
165+
values.to_a
166+
elsif values.is_a?(Proc)
167+
values.call
168+
else
169+
values
170+
end
171+
end
172+
163173
def create_documentation_class
164174
Class.new(Grape::API) do
165175
class << self
@@ -214,9 +224,8 @@ def parse_params(params, path, method)
214224
default_value = value.is_a?(Hash) ? value[:default] : nil
215225
example = value.is_a?(Hash) ? value[:example] : nil
216226
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
217-
enum_values = value.is_a?(Hash) ? value[:values] : nil
218-
enum_values = enum_values.to_a if enum_values && enum_values.is_a?(Range)
219-
enum_values = enum_values.call if enum_values && enum_values.is_a?(Proc)
227+
values = value.is_a?(Hash) ? value[:values] : nil
228+
enum_values = parse_enum_values(values)
220229

221230
if value.is_a?(Hash) && value.key?(:documentation) && value[:documentation].key?(:param_type)
222231
param_type = value[:documentation][:param_type]

spec/param_values_spec.rb

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
require 'spec_helper'
2+
require 'grape_version'
3+
4+
describe 'Convert values to enum' do
5+
def app
6+
Class.new(Grape::API) do
7+
format :json
8+
9+
params do
10+
requires :letter, type: String, values: %w(a b c)
11+
end
12+
post :plain_array do
13+
end
14+
15+
params do
16+
requires :letter, type: String, values: proc { %w(d e f) }
17+
end
18+
post :array_in_proc do
19+
end
20+
21+
params do
22+
requires :letter, type: String, values: 'a'..'z'
23+
end
24+
post :range_letter do
25+
end
26+
27+
params do
28+
requires :integer, type: Integer, values: -5..5
29+
end
30+
post :range_integer do
31+
end
32+
33+
add_swagger_documentation
34+
end
35+
end
36+
37+
def first_parameter_info(request)
38+
get "/swagger_doc/#{request}"
39+
expect(last_response.status).to eq 200
40+
body = JSON.parse last_response.body
41+
body['apis'].first['operations'].first['parameters']
42+
end
43+
44+
context 'Plain array values' do
45+
subject(:plain_array) { first_parameter_info('plain_array') }
46+
47+
it 'has values as array in enum' do
48+
expect(plain_array).to eq [
49+
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => %w(a b c) }
50+
]
51+
end
52+
end
53+
54+
context 'Array in proc values' do
55+
subject(:array_in_proc) { first_parameter_info('array_in_proc') }
56+
57+
it 'has proc returned values as array in enum' do
58+
expect(array_in_proc).to eq [
59+
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => %w(d e f) }
60+
]
61+
end
62+
end
63+
64+
context 'Range values' do
65+
subject(:range_letter) { first_parameter_info('range_letter') }
66+
67+
it 'has letter range values as array in enum' do
68+
expect(range_letter).to eq [
69+
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => ('a'..'z').to_a }
70+
]
71+
end
72+
73+
subject(:range_integer) { first_parameter_info('range_integer') }
74+
75+
it 'has integer range values as array in enum' do
76+
expect(range_integer).to eq [
77+
{ 'paramType' => 'form', 'name' => 'integer', 'description' => nil, 'type' => 'integer', 'required' => true, 'allowMultiple' => false, 'format' => 'int32', 'enum' => (-5..5).to_a }
78+
]
79+
end
80+
end
81+
end
82+
83+
describe 'Convert values to enum for float range and not arrays inside a proc', if: GrapeVersion.satisfy?('>= 0.11.0') do
84+
def app
85+
Class.new(Grape::API) do
86+
format :json
87+
88+
params do
89+
requires :letter, type: String, values: proc { 'string' }
90+
end
91+
post :non_array_in_proc do
92+
end
93+
94+
params do
95+
requires :float, type: Float, values: -5.0..5.0
96+
end
97+
post :range_float do
98+
end
99+
100+
add_swagger_documentation
101+
end
102+
end
103+
104+
def first_parameter_info(request)
105+
get "/swagger_doc/#{request}"
106+
expect(last_response.status).to eq 200
107+
body = JSON.parse last_response.body
108+
body['apis'].first['operations'].first['parameters']
109+
end
110+
111+
context 'Non array in proc values' do
112+
subject(:non_array_in_proc) { first_parameter_info('non_array_in_proc') }
113+
114+
it 'has proc returned value as string in enum' do
115+
expect(non_array_in_proc).to eq [
116+
{ 'paramType' => 'form', 'name' => 'letter', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false, 'enum' => 'string' }
117+
]
118+
end
119+
end
120+
121+
context 'Range values' do
122+
subject(:range_float) { first_parameter_info('range_float') }
123+
124+
it 'has float range values as string in enum' do
125+
expect(range_float).to eq [
126+
{ 'paramType' => 'form', 'name' => 'float', 'description' => nil, 'type' => 'float', 'required' => true, 'allowMultiple' => false, 'enum' => '-5.0..5.0' }
127+
]
128+
end
129+
end
130+
end

spec/range_values_spec.rb

Lines changed: 0 additions & 49 deletions
This file was deleted.

spec/support/grape_version.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class GrapeVersion
2+
class << self
3+
def current_version
4+
Grape::VERSION
5+
end
6+
7+
def satisfy?(requirement)
8+
Gem::Dependency.new('grape-test', requirement).match?('grape-test', current_version)
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)