Skip to content

Fix issue where array params are sometimes omitted when array_use_braces is true #957

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def call(param, settings, path, route, definitions, consumes) # rubocop:disable
in: param_type(value_type, consumes),
name: settings[:full_name] || param
}
fix_name_of_body_array_param(value_type)

# optional properties
document_description(settings)
Expand All @@ -35,6 +36,12 @@ def call(param, settings, path, route, definitions, consumes) # rubocop:disable

private

def fix_name_of_body_array_param(value_type)
return unless @parsed_param[:in] == 'body' && value_type[:is_array]

@parsed_param[:name] = @parsed_param[:name].delete_suffix('[]')
end

def document_description(settings)
description = settings[:desc] || settings[:description]
@parsed_param[:description] = description if description
Expand Down
68 changes: 68 additions & 0 deletions spec/swagger_v2/params_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,27 @@
{ 'declared_params' => declared(params) }
end

helpers do
params :common_params do
requires :array_of_string, type: Array[String]
requires :array_of_integer, type: Array[Integer]
end
end

params do
use :common_params
end
get '/endpoint_with_common_params' do
{ 'declared_params' => declared(params) }
end

params do
use :common_params
end
post '/endpoint_with_common_params' do
{ 'declared_params' => declared(params) }
end

add_swagger_documentation array_use_braces: array_use_braces
end
end
Expand Down Expand Up @@ -220,6 +241,53 @@
expect(subject['paths']['/array_of_entities']['post']['parameters']).to eql(parameters)
end
end

describe 'retrieves the documentation for parameters shared between GET and POST endpoints' do
subject do
get '/swagger_doc/endpoint_with_common_params'
JSON.parse(last_response.body)
end

describe 'for non-body parameters' do
specify do
expect(subject['paths']['/endpoint_with_common_params']['get']['parameters']).to eql(
[
{ 'in' => 'query', 'name' => "array_of_string#{braces}", 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
{ 'in' => 'query', 'name' => "array_of_integer#{braces}", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
]
)
end
end

describe 'for body parameters' do
specify do
expect(subject['paths']['/endpoint_with_common_params']['post']['parameters']).to eql(
[
{ 'in' => 'body', 'name' => 'postEndpointWithCommonParams', 'schema' => { '$ref' => '#/definitions/postEndpointWithCommonParams' }, 'required' => true }
]
)
expect(subject['definitions']['postEndpointWithCommonParams']).to eql(
'type' => 'object',
'properties' => {
'array_of_string' => { # no braces, even if array_use_braces is true
'type' => 'array',
'items' => {
'type' => 'string'
}
},
'array_of_integer' => { # no braces, even if array_use_braces is true
'type' => 'array',
'items' => {
'type' => 'integer',
'format' => 'int32'
}
}
},
'required' => %w[array_of_string array_of_integer]
)
end
end
end
end
end
end
Loading