Skip to content

Commit a5e2575

Browse files
authored
Document the min/max Items/Length fields if the attribute uses the length validator (#934)
* Document the min/max Items/Length fields if the attribute uses the length validator. * restrict rack version to less than 3.0 when grape version 1.8 or lower * fix spec
1 parent 707b00b commit a5e2575

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

Gemfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ group :development, :test do
1919
gem 'pry', platforms: [:mri]
2020
gem 'pry-byebug', platforms: [:mri]
2121

22-
gem 'rack'
22+
grape_version = ENV.fetch('GRAPE_VERSION', '2.1.0')
23+
if grape_version == 'HEAD' || Gem::Version.new(grape_version) >= Gem::Version.new('2.0.0')
24+
gem 'rack', '>= 3.0'
25+
else
26+
gem 'rack', '< 3.0'
27+
end
28+
2329
gem 'rack-cors'
2430
gem 'rack-test'
2531
gem 'rake'

lib/grape-swagger/doc_methods/parse_params.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def call(param, settings, path, route, definitions, consumes) # rubocop:disable
2525
document_default_value(settings) unless value_type[:is_array]
2626
document_range_values(settings) unless value_type[:is_array]
2727
document_required(settings)
28+
document_length_limits(value_type)
2829
document_additional_properties(definitions, settings) unless value_type[:is_array]
2930
document_add_extensions(settings)
3031
document_example(settings)
@@ -163,6 +164,16 @@ def param_type(value_type, consumes)
163164
end
164165
end
165166

167+
def document_length_limits(value_type)
168+
if value_type[:is_array]
169+
@parsed_param[:minItems] = value_type[:min_length] if value_type.key?(:min_length)
170+
@parsed_param[:maxItems] = value_type[:max_length] if value_type.key?(:max_length)
171+
else
172+
@parsed_param[:minLength] = value_type[:min_length] if value_type.key?(:min_length)
173+
@parsed_param[:maxLength] = value_type[:max_length] if value_type.key?(:max_length)
174+
end
175+
end
176+
166177
def parse_enum_or_range_values(values)
167178
case values
168179
when Proc

spec/issues/533_specify_status_code_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
success: { code: 204, message: 'a changed status code' }
2727
patch do
2828
status 204
29+
body false
2930
end
3031

3132
desc 'Delete some stuff',

spec/swagger_v2/param_type_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,51 @@ def app
2727
{ message: 'hi' }
2828
end
2929

30+
if Gem::Version.new(Grape::VERSION) >= Gem::Version.new('2.1.0')
31+
desc 'other_action' do
32+
consumes ['application/x-www-form-urlencoded']
33+
end
34+
params do
35+
requires :input, type: String, length: { min: 1, max: 12 }
36+
requires :arr, type: [Integer], length: { min: 1, max: 12 }
37+
end
38+
post :other_action do
39+
{ message: 'hi' }
40+
end
41+
end
42+
3043
add_swagger_documentation
3144
end
3245
end
46+
47+
context 'when length validator is used', if: Gem::Version.new(Grape::VERSION) >= Gem::Version.new('2.1.0') do
48+
subject do
49+
get '/swagger_doc/other_action'
50+
end
51+
52+
it 'documents the length/item limits correctly' do
53+
subject
54+
55+
expect(last_response.status).to eq 200
56+
expect(JSON.parse(last_response.body)['paths']['/other_action']['post']['parameters']).to eq([{
57+
'in' => 'formData',
58+
'maxLength' => 12,
59+
'minLength' => 1,
60+
'name' => 'input',
61+
'required' => true,
62+
'type' => 'string'
63+
}, {
64+
'in' => 'formData',
65+
'items' => { 'format' => 'int32', 'type' => 'integer' },
66+
'maxItems' => 12,
67+
'minItems' => 1,
68+
'name' => 'arr',
69+
'required' => true,
70+
'type' => 'array'
71+
}])
72+
end
73+
end
74+
3375
context 'with no documentation hash' do
3476
subject do
3577
get '/swagger_doc/action'

0 commit comments

Comments
 (0)