diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f1eabc45..e3de2c94 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -7,12 +7,12 @@ # Offense count: 9 Metrics/AbcSize: - Max: 346 + Max: 347 # Offense count: 1 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 466 + Max: 485 # Offense count: 6 Metrics/CyclomaticComplexity: @@ -26,7 +26,7 @@ Metrics/LineLength: # Offense count: 17 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 367 + Max: 368 # Offense count: 5 Metrics/PerceivedComplexity: diff --git a/CHANGELOG.md b/CHANGELOG.md index c8d6587d..f576abe8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ #### Fixes +* [#221](https://github.com/tim-vandecasteele/grape-swagger/pull/221): Fixed group parameters' name with type Array - [@u2](https://github.com/u2). * [#211](https://github.com/tim-vandecasteele/grape-swagger/pull/211): Fixed the dependency, just `require 'grape'` - [@u2](https://github.com/u2). * [#210](https://github.com/tim-vandecasteele/grape-swagger/pull/210): Fixed the range `:values` option, now exposed as `enum` parameters - [@u2](https://github.com/u2). * [#208](https://github.com/tim-vandecasteele/grape-swagger/pull/208): Fixed `Float` parameters, exposed as Swagger `float` types - [@u2](https://github.com/u2). diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 3cafd832..004aadb2 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -139,6 +139,25 @@ def get_non_nested_params(params) end end + def parse_array_params(params) + modified_params = {} + array_param = nil + params.each_key do |k| + if params[k].is_a?(Hash) && params[k][:type] == 'Array' + array_param = k + else + new_key = k + unless array_param.nil? + if k.to_s.start_with?(array_param.to_s + '[') + new_key = array_param.to_s + '[]' + k.to_s.split(array_param)[1] + end + end + modified_params[new_key] = params[k] + end + end + modified_params + end + def create_documentation_class Class.new(Grape::API) do class << self @@ -153,7 +172,9 @@ def as_markdown(description) def parse_params(params, path, method) params ||= [] - non_nested_parent_params = get_non_nested_params(params) + parsed_array_params = parse_array_params(params) + + non_nested_parent_params = get_non_nested_params(parsed_array_params) non_nested_parent_params.map do |param, value| items = {} diff --git a/spec/array_params_spec.rb b/spec/array_params_spec.rb new file mode 100644 index 00000000..071ba557 --- /dev/null +++ b/spec/array_params_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe 'Array Params' do + def app + Class.new(Grape::API) do + format :json + + params do + requires :a_array, type: Array do + requires :param_1, type: Integer + requires :param_2, type: String + end + end + post :splines do + end + + add_swagger_documentation + end + end + + subject do + get '/swagger_doc/splines' + expect(last_response.status).to eq 200 + body = JSON.parse last_response.body + body['apis'].first['operations'].first['parameters'] + end + + it 'gets array types' do + expect(subject).to eq [ + { 'paramType' => 'form', 'name' => 'a_array[][param_1]', 'description' => nil, 'type' => 'integer', 'required' => true, 'allowMultiple' => false, 'format' => 'int32' }, + { 'paramType' => 'form', 'name' => 'a_array[][param_2]', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false } + ] + end +end diff --git a/spec/float_api_spec.rb b/spec/float_api_spec.rb index 6f69fe28..adc9cd28 100644 --- a/spec/float_api_spec.rb +++ b/spec/float_api_spec.rb @@ -6,7 +6,7 @@ def app format :json params do - requires :a_float, type: Virtus::Attribute::Float + requires :a_float, type: Float end post :splines do end diff --git a/spec/mutually_exclusive_spec.rb b/spec/mutually_exclusive_spec.rb index 5582bd57..faccc342 100644 --- a/spec/mutually_exclusive_spec.rb +++ b/spec/mutually_exclusive_spec.rb @@ -31,6 +31,6 @@ def app parameters = body['apis'].first['operations'].first['parameters'] expect(parameters).to eq [ { 'paramType' => 'form', 'name' => 'required_group[param_group_1][param_1]', 'description' => nil, 'type' => 'string', 'required' => false, 'allowMultiple' => false }, - { 'paramType' => 'form', 'name' => 'required_group[param_group_2][param_2]', 'description' => nil, 'type' => 'string', 'required' => false, 'allowMultiple' => false }] + { 'paramType' => 'form', 'name' => 'required_group[param_group_2][][param_2]', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false }] end end diff --git a/spec/range_values_spec.rb b/spec/range_values_spec.rb index 6f07490d..1e9264b3 100644 --- a/spec/range_values_spec.rb +++ b/spec/range_values_spec.rb @@ -6,13 +6,13 @@ def app format :json params do - requires :letter, type: Virtus::Attribute::String, values: 'a'..'z' + requires :letter, type: String, values: 'a'..'z' end post :letter do end params do - requires :number, type: Virtus::Attribute::Integer, values: -5..5 + requires :number, type: Integer, values: -5..5 end post :integer do end