Skip to content

Commit

Permalink
Document the min/max Items/Length fields if the attribute uses the le…
Browse files Browse the repository at this point in the history
…ngth validator.
  • Loading branch information
dhruvCW committed Jun 22, 2024
1 parent 707b00b commit 2f6fb72
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def call(param, settings, path, route, definitions, consumes) # rubocop:disable
document_default_value(settings) unless value_type[:is_array]
document_range_values(settings) unless value_type[:is_array]
document_required(settings)
document_length_limits(value_type)
document_additional_properties(definitions, settings) unless value_type[:is_array]
document_add_extensions(settings)
document_example(settings)
Expand Down Expand Up @@ -163,6 +164,16 @@ def param_type(value_type, consumes)
end
end

def document_length_limits(value_type)
if value_type[:is_array]
@parsed_param[:minItems] = value_type[:min_length] if value_type.key?(:min_length)
@parsed_param[:maxItems] = value_type[:max_length] if value_type.key?(:max_length)
else
@parsed_param[:minLength] = value_type[:min_length] if value_type.key?(:min_length)
@parsed_param[:maxLength] = value_type[:max_length] if value_type.key?(:max_length)
end
end

def parse_enum_or_range_values(values)
case values
when Proc
Expand Down
42 changes: 42 additions & 0 deletions spec/swagger_v2/param_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,51 @@ def app
{ message: 'hi' }
end

if Gem::Version.new(Grape::VERSION) >= Gem::Version.new('2.1.0')
desc 'other_action' do
consumes ['application/x-www-form-urlencoded']
end
params do
requires :input, type: String, length: { min: 1, max: 12 }
requires :arr, type: [Integer], length: { min: 1, max: 12 }
end
post :other_action do
{ message: 'hi' }
end
end

add_swagger_documentation
end
end

context 'when length validator is used', if: Gem::Version.new(Grape::VERSION) >= Gem::Version.new('2.1.0') do
subject do
get '/swagger_doc/other_action'
end

it 'documents the length/item limits correctly' do
subject

expect(last_response.status).to eq 200
expect(JSON.parse(last_response.body)['paths']['/other_action']['post']['parameters']).to eq([{
'in' => 'formData',
'maxLength' => 12,
'minLength' => 1,
'name' => 'input',
'required' => true,
'type' => 'string'
}, {
'in' => 'formData',
'items' => { 'format' => 'int32', 'type' => 'integer' },
'maxItems' => 12,
'minItems' => 1,
'name' => 'arr',
'required' => true,
'type' => 'array'
}])
end
end

context 'with no documentation hash' do
subject do
get '/swagger_doc/action'
Expand Down

0 comments on commit 2f6fb72

Please sign in to comment.