Skip to content

Commit f427bf7

Browse files
committed
Document the min/max Items/Length fields if the attribute uses the length validator.
1 parent 707b00b commit f427bf7

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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/swagger_v2/param_type_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,49 @@ 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 :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?input=test&arr=1'
50+
end
51+
52+
it 'documents the length/item limits correctly' do
53+
expect(last_response.status).to eq 200
54+
expect(JSON.parse(last_response.body['paths']['/action']['post']['parameters'])).to eq([{
55+
'in' => 'formData',
56+
'maxLength' => 12,
57+
'minLength' => 1,
58+
'name' => 'input',
59+
'required' => true,
60+
'type' => 'string'
61+
}, {
62+
'in' => 'formData',
63+
'items' => { 'format' => 'int32', 'type' => 'integer' },
64+
'maxItems' => 12,
65+
'minItems' => 1,
66+
'name' => 'arr',
67+
'required' => true,
68+
'type' => 'array'
69+
}])
70+
end
71+
end
72+
3373
context 'with no documentation hash' do
3474
subject do
3575
get '/swagger_doc/action'

0 commit comments

Comments
 (0)