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
This commit is contained in:
Dhruv Paranjape 2024-06-28 09:16:20 +02:00 committed by GitHub
parent 707b00b91b
commit a5e2575a02
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 1 deletions

View File

@ -19,7 +19,13 @@ group :development, :test do
gem 'pry', platforms: [:mri]
gem 'pry-byebug', platforms: [:mri]
gem 'rack'
grape_version = ENV.fetch('GRAPE_VERSION', '2.1.0')
if grape_version == 'HEAD' || Gem::Version.new(grape_version) >= Gem::Version.new('2.0.0')
gem 'rack', '>= 3.0'
else
gem 'rack', '< 3.0'
end
gem 'rack-cors'
gem 'rack-test'
gem 'rake'

View File

@ -25,6 +25,7 @@ module GrapeSwagger
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)
@ -163,6 +164,16 @@ module GrapeSwagger
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

View File

@ -26,6 +26,7 @@ describe '#533 specify status codes' do
success: { code: 204, message: 'a changed status code' }
patch do
status 204
body false
end
desc 'Delete some stuff',

View File

@ -27,9 +27,51 @@ describe 'Params Types' do
{ 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'