Fix group parameters' name with type Array.

This commit is contained in:
zhangyaning1985@gmail.com 2015-03-03 06:52:26 -05:00 committed by dB
parent a0d446daae
commit 12dae157e9
7 changed files with 64 additions and 8 deletions

View File

@ -7,12 +7,12 @@
# Offense count: 9 # Offense count: 9
Metrics/AbcSize: Metrics/AbcSize:
Max: 346 Max: 347
# Offense count: 1 # Offense count: 1
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/ClassLength: Metrics/ClassLength:
Max: 466 Max: 485
# Offense count: 6 # Offense count: 6
Metrics/CyclomaticComplexity: Metrics/CyclomaticComplexity:
@ -26,7 +26,7 @@ Metrics/LineLength:
# Offense count: 17 # Offense count: 17
# Configuration parameters: CountComments. # Configuration parameters: CountComments.
Metrics/MethodLength: Metrics/MethodLength:
Max: 367 Max: 368
# Offense count: 5 # Offense count: 5
Metrics/PerceivedComplexity: Metrics/PerceivedComplexity:

View File

@ -11,6 +11,7 @@
#### Fixes #### 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). * [#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). * [#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). * [#208](https://github.com/tim-vandecasteele/grape-swagger/pull/208): Fixed `Float` parameters, exposed as Swagger `float` types - [@u2](https://github.com/u2).

View File

@ -139,6 +139,25 @@ module Grape
end end
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 def create_documentation_class
Class.new(Grape::API) do Class.new(Grape::API) do
class << self class << self
@ -153,7 +172,9 @@ module Grape
def parse_params(params, path, method) def parse_params(params, path, method)
params ||= [] 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| non_nested_parent_params.map do |param, value|
items = {} items = {}

34
spec/array_params_spec.rb Normal file
View File

@ -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

View File

@ -6,7 +6,7 @@ describe 'Float Params' do
format :json format :json
params do params do
requires :a_float, type: Virtus::Attribute::Float requires :a_float, type: Float
end end
post :splines do post :splines do
end end

View File

@ -31,6 +31,6 @@ describe 'Mutually exclusive group params' do
parameters = body['apis'].first['operations'].first['parameters'] parameters = body['apis'].first['operations'].first['parameters']
expect(parameters).to eq [ 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_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
end end

View File

@ -6,13 +6,13 @@ describe 'Range Params' do
format :json format :json
params do params do
requires :letter, type: Virtus::Attribute::String, values: 'a'..'z' requires :letter, type: String, values: 'a'..'z'
end end
post :letter do post :letter do
end end
params do params do
requires :number, type: Virtus::Attribute::Integer, values: -5..5 requires :number, type: Integer, values: -5..5
end end
post :integer do post :integer do
end end