Set query parameter for array of primitive types (#929)

* 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>
This commit is contained in:
Dhruv Paranjape 2024-05-14 13:09:05 +02:00 committed by GitHub
parent e7856de6d0
commit 95be315986
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 62 additions and 9 deletions

View File

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

View File

@ -75,6 +75,14 @@ module GrapeSwagger
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

View File

@ -70,21 +70,17 @@ module GrapeSwagger
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)
@ -150,7 +146,7 @@ module GrapeSwagger
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
@ -160,6 +156,8 @@ module GrapeSwagger
else
'body'
end
elsif value_type[:is_array] && !DataType.query_array_primitive?(value_type[:data_type])
'formData'
else
'query'
end

View File

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

View File

@ -52,7 +52,7 @@ describe 'Group Array Params, using collection format' do
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
@ -67,7 +67,7 @@ describe 'Group Array Params, using collection format' do
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
@ -82,7 +82,7 @@ describe 'Group Array Params, using collection format' do
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
@ -97,7 +97,7 @@ describe 'Group Array Params, using collection format' do
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