Skip to content
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

Set query parameter for array of primitive types #929

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
31 changes: 25 additions & 6 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) # rubocop:disable
# 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]
LeFnord marked this conversation as resolved.
Show resolved Hide resolved
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,47 @@ 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] || 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[: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)
if value_type[:documentation].present?
param_type = value_type[:documentation][:param_type] || value_type[:documentation][:in]
end

param_type ||= value_type[:param_type]

if param_type
param_type
elsif %w[POST PUT PATCH].include?(value_type[:method])
if consumes.include?('application/x-www-form-urlencoded') || consumes.include?('multipart/form-data')
'formData'
else
'body'
end
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