From c36e286dc7fba4915b9b909b5ada6c2d94706533 Mon Sep 17 00:00:00 2001 From: Yun-Hui Fan Date: Mon, 17 Nov 2014 15:59:36 -0800 Subject: [PATCH 1/2] merge and fix conflicts --- lib/grape-swagger.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 3cafd832..0f8a46b6 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -541,12 +541,28 @@ def setup(options) models = @@documentation_class.models_with_included_presenters(models.flatten.compact) + modified_route_params = {} + array_param = nil + route.route_params.each_key do |k| + if route.route_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_route_params[new_key] = route.route_params[k] + end + end + operation = { notes: notes.to_s, summary: route.route_description || '', nickname: route.route_nickname || (route.route_method + route.route_path.gsub(/[\/:\(\)\.]/, '-')), method: route.route_method, - parameters: @@documentation_class.parse_header_params(route.route_headers) + @@documentation_class.parse_params(route.route_params, route.route_path, route.route_method), + parameters: @@documentation_class.parse_header_params(route.route_headers) + @@documentation_class.parse_params(modified_route_params, route.route_path, route.route_method), type: 'void' } operation[:authorizations] = route.route_authorizations unless route.route_authorizations.nil? || route.route_authorizations.empty? From 226736cf3722f5e9602bacdd0d648558b523d5e2 Mon Sep 17 00:00:00 2001 From: "zhangyaning1985@gmail.com" Date: Fri, 27 Feb 2015 14:36:53 +0800 Subject: [PATCH 2/2] Fix group parameters' name with type Array --- .rubocop_todo.yml | 6 ++--- CHANGELOG.md | 1 + lib/grape-swagger.rb | 41 ++++++++++++++++++--------------- spec/array_params_spec.rb | 34 +++++++++++++++++++++++++++ spec/float_api_spec.rb | 2 +- spec/mutually_exclusive_spec.rb | 2 +- spec/range_values_spec.rb | 4 ++-- 7 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 spec/array_params_spec.rb 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 0f8a46b6..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 = {} @@ -541,28 +562,12 @@ def setup(options) models = @@documentation_class.models_with_included_presenters(models.flatten.compact) - modified_route_params = {} - array_param = nil - route.route_params.each_key do |k| - if route.route_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_route_params[new_key] = route.route_params[k] - end - end - operation = { notes: notes.to_s, summary: route.route_description || '', nickname: route.route_nickname || (route.route_method + route.route_path.gsub(/[\/:\(\)\.]/, '-')), method: route.route_method, - parameters: @@documentation_class.parse_header_params(route.route_headers) + @@documentation_class.parse_params(modified_route_params, route.route_path, route.route_method), + parameters: @@documentation_class.parse_header_params(route.route_headers) + @@documentation_class.parse_params(route.route_params, route.route_path, route.route_method), type: 'void' } operation[:authorizations] = route.route_authorizations unless route.route_authorizations.nil? || route.route_authorizations.empty? 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