diff --git a/lib/grape-swagger/doc_methods/parse_params.rb b/lib/grape-swagger/doc_methods/parse_params.rb index b416c020..c4a1b804 100644 --- a/lib/grape-swagger/doc_methods/parse_params.rb +++ b/lib/grape-swagger/doc_methods/parse_params.rb @@ -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) @@ -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 diff --git a/spec/swagger_v2/param_type_spec.rb b/spec/swagger_v2/param_type_spec.rb index 4dde18f9..a09da6b8 100644 --- a/spec/swagger_v2/param_type_spec.rb +++ b/spec/swagger_v2/param_type_spec.rb @@ -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'