Skip to content

Commit 20b87e9

Browse files
committed
Fixed the range :values options, now it does not fail with float range
now range values is exposed as array in enum field only if it is Integer or String range float range or procs returning non-array values is exposed as string fixes #233
1 parent daaad4c commit 20b87e9

File tree

6 files changed

+130
-61
lines changed

6 files changed

+130
-61
lines changed

.rubocop_todo.yml

Lines changed: 9 additions & 9 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-17 18:01:25 +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: 501
1616

17-
# Offense count: 6
17+
# Offense count: 7
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 the range :values options, now it does not fail with float range - [@azhi](https://github.com/azhi).
1213

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

lib/grape-swagger.rb

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

163+
def parse_enum_values(values)
164+
enum_values = values
165+
enum_values = enum_values.to_a if enum_values && enum_values.is_a?(Range) && [Integer, String].any? { |klass| enum_values.first.is_a?(klass) }
166+
enum_values = enum_values.call if enum_values && enum_values.is_a?(Proc)
167+
enum_values
168+
end
169+
163170
def create_documentation_class
164171
Class.new(Grape::API) do
165172
class << self
@@ -214,9 +221,8 @@ def parse_params(params, path, method)
214221
default_value = value.is_a?(Hash) ? value[:default] : nil
215222
example = value.is_a?(Hash) ? value[:example] : nil
216223
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)
224+
values = value.is_a?(Hash) ? value[:values] : nil
225+
enum_values = parse_enum_values(values)
220226

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

spec/params_values_spec.rb

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

spec/range_values_spec.rb

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

0 commit comments

Comments
 (0)