Skip to content

Commit

Permalink
Set query parameter for array of primitive types
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceraccoon committed Nov 21, 2022
1 parent e04dc97 commit 91c74e9
Show file tree
Hide file tree
Showing 5 changed files with 75 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

* [#880](https://github.com/ruby-grape/grape-swagger/pull/880): Set default parameter location based on consumes - [@spaceraccoon](https://github.com/spaceraccoon)
* [#886](https://github.com/ruby-grape/grape-swagger/pull/884): 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
21 changes: 16 additions & 5 deletions lib/grape-swagger/doc_methods/parse_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call(param, settings, path, route, definitions, consumes)
# optional properties
document_description(settings)
document_type_and_format(settings, data_type)
document_array_param(value_type, definitions) if value_type[:is_array]
document_array_param(value_type, definitions, consumes) if value_type[:is_array]
document_default_value(settings) unless value_type[:is_array]
document_range_values(settings) unless value_type[:is_array]
document_required(settings)
Expand Down Expand Up @@ -68,28 +68,39 @@ def document_add_extensions(settings)
GrapeSwagger::DocMethods::Extensions.add_extensions_to_root(settings, @parsed_param)
end

def document_array_param(value_type, definitions)
def document_array_param(value_type, definitions, consumes)
if value_type[:documentation].present?
param_type = value_type[:documentation][:param_type]
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[:in] = array_param_type(value_type, consumes)
@parsed_param[:items] = array_items
@parsed_param[:type] = 'array'
@parsed_param[:collectionFormat] = collection_format if DataType.collections.include?(collection_format)
end

def array_param_type(value_type, consumes)
param_type = value_type[:param_type] || value_type[:in]
if param_type
param_type
elsif %w[POST PUT PATCH].include?(value_type[:method])
consumes.include?('application/x-www-form-urlencoded') || consumes.include?('multipart/form-data') ? 'formData' : 'body'
elsif DataType.query_array_primitive?(value_type[:data_type])
'query'
else
'formData'
end
end

def parse_array_item(definitions, type, value_type)
array_items = {}
if definitions[value_type[:data_type]]
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 91c74e9

Please sign in to comment.