Skip to content

Document the min/max Items/Length fields if the attribute uses the length validator #934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
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
1 change: 1 addition & 0 deletions spec/issues/533_specify_status_code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
success: { code: 204, message: 'a changed status code' }
patch do
status 204
body false
end

desc 'Delete some stuff',
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