Skip to content

Fix group parameters' name with type Array #221

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -26,7 +26,7 @@ Metrics/LineLength:
# Offense count: 17
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 367
Max: 368

# Offense count: 5
Metrics/PerceivedComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
23 changes: 22 additions & 1 deletion lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {}
Expand Down
34 changes: 34 additions & 0 deletions spec/array_params_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/float_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/mutually_exclusive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/range_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down