Skip to content

Commit

Permalink
Set query parameter for array of primitive types (#929)
Browse files Browse the repository at this point in the history
* Set query parameter for array of primitive types

* Update PR number in changelog

* refactor param_type method to handle array params.

---------

Co-authored-by: Eugene Lim <limzhiweieugene@gmail.com>
  • Loading branch information
dhruvCW and spaceraccoon authored May 14, 2024
1 parent e7856de commit 95be315
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* [#927](https://github.com/ruby-grape/grape-swagger/pull/927): Set default parameter location based on consumes - [@spaceraccoon](https://github.com/spaceraccoon)
* [#929](https://github.com/ruby-grape/grape-swagger/pull/929): Set query parameter for array of primitive types - [@spaceraccoon](https://github.com/spaceraccoon)
* Your contribution here.

#### Fixes
Expand Down
8 changes: 8 additions & 0 deletions lib/grape-swagger/doc_methods/data_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def primitives
PRIMITIVE_MAPPINGS.keys.map(&:downcase)
end

def query_array_primitive?(type)
query_array_primitives.include?(type.to_s.downcase)
end

def query_array_primitives
primitives << 'string'
end

def mapping(value)
PRIMITIVE_MAPPINGS[value] || 'string'
end
Expand Down
8 changes: 3 additions & 5 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,17 @@ def document_add_extensions(settings)

def document_array_param(value_type, definitions)
if value_type[:documentation].present?
param_type = value_type[:documentation][:param_type] || value_type[:documentation][:in]
doc_type = value_type[:documentation][:type]
type = DataType.mapping(doc_type) if doc_type && !DataType.request_primitive?(doc_type)
collection_format = value_type[:documentation][:collectionFormat]
end

param_type ||= value_type[:param_type]

array_items = parse_array_item(
definitions,
type,
value_type
)

@parsed_param[:in] = param_type || 'formData'
@parsed_param[:items] = array_items
@parsed_param[:type] = 'array'
@parsed_param[:collectionFormat] = collection_format if DataType.collections.include?(collection_format)
Expand Down Expand Up @@ -150,7 +146,7 @@ def document_example(settings)

def param_type(value_type, consumes)
param_type = value_type[:param_type] || value_type[:in]
if value_type[:path].include?("{#{value_type[:param_name]}}")
if !value_type[:is_array] && value_type[:path].include?("{#{value_type[:param_name]}}")
'path'
elsif param_type
param_type
Expand All @@ -160,6 +156,8 @@ def param_type(value_type, consumes)
else
'body'
end
elsif value_type[:is_array] && !DataType.query_array_primitive?(value_type[:data_type])
'formData'
else
'query'
end
Expand Down
46 changes: 46 additions & 0 deletions spec/issues/883_query_array_parameter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require 'spec_helper'

describe '#883 Group Params as Array' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_883 do
params do
requires :array_of_string, type: [String]
requires :array_of_integer, type: [Integer]
end
get '/get_primitive_array_parameters' do
'accepts array query parameters of primitive value types'
end

params do
requires :array_of, type: Array, documentation: { type: 'link', is_array: true }
end
get '/get_object_array_parameters' do
'does not accept array query parameters of object value types'
end
end
add_swagger_documentation
end
end

describe 'retrieves the documentation for typed group range parameters' do
subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

specify do
expect(subject['paths']['/issue_883/get_primitive_array_parameters']['get']['parameters']).to eql(
[
{ 'in' => 'query', 'name' => 'array_of_string', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
{ 'in' => 'query', 'name' => 'array_of_integer', 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
]
)
expect(subject['paths']['/issue_883/get_object_array_parameters']['get']['parameters']).to eql(
[{ 'in' => 'formData', 'items' => { 'type' => 'string' }, 'name' => 'array_of', 'required' => true, 'type' => 'array' }]
)
end
end
end
8 changes: 4 additions & 4 deletions spec/swagger_v2/params_array_collection_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def app
specify do
expect(subject['paths']['/array_of_strings_without_collection_format']['get']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'description' => 'array in csv collection format' }
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'description' => 'array in csv collection format' }
]
)
end
Expand All @@ -67,7 +67,7 @@ def app
specify do
expect(subject['paths']['/array_of_strings_multi_collection_format']['get']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'multi', 'description' => 'array in multi collection format' }
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'multi', 'description' => 'array in multi collection format' }
]
)
end
Expand All @@ -82,7 +82,7 @@ def app
specify do
expect(subject['paths']['/array_of_strings_brackets_collection_format']['get']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'brackets', 'description' => 'array in brackets collection format' }
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false, 'collectionFormat' => 'brackets', 'description' => 'array in brackets collection format' }
]
)
end
Expand All @@ -97,7 +97,7 @@ def app
specify do
expect(subject['paths']['/array_of_strings_invalid_collection_format']['get']['parameters']).to eql(
[
{ 'in' => 'formData', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
{ 'in' => 'query', 'name' => 'array_of_strings', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
]
)
end
Expand Down

0 comments on commit 95be315

Please sign in to comment.