2017-03-08 16:32:31 +08:00
|
|
|
# frozen_string_literal: true
|
2017-03-28 17:15:33 +08:00
|
|
|
|
2015-11-04 20:03:42 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe 'Group Params as Array' do
|
2016-10-14 05:06:03 +08:00
|
|
|
include_context "#{MODEL_PARSER} swagger example"
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
[true, false].each do |array_use_braces|
|
|
|
|
context "when array_use_braces option is set to #{array_use_braces}" do
|
|
|
|
let(:braces) { array_use_braces ? '[]' : '' }
|
|
|
|
|
|
|
|
let(:app) do
|
|
|
|
Class.new(Grape::API) do
|
|
|
|
format :json
|
|
|
|
|
2024-05-10 18:42:54 +08:00
|
|
|
desc 'groups' do
|
|
|
|
consumes ['application/x-www-form-urlencoded']
|
|
|
|
end
|
2018-08-21 19:28:33 +08:00
|
|
|
params do
|
|
|
|
requires :required_group, type: Array do
|
|
|
|
requires :required_param_1
|
|
|
|
requires :required_param_2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
post '/groups' do
|
|
|
|
{ 'declared_params' => declared(params) }
|
|
|
|
end
|
|
|
|
|
2024-05-10 18:42:54 +08:00
|
|
|
desc 'type_given' do
|
|
|
|
consumes ['application/x-www-form-urlencoded']
|
|
|
|
end
|
2018-08-21 19:28:33 +08:00
|
|
|
params do
|
|
|
|
requires :typed_group, type: Array do
|
|
|
|
requires :id, type: Integer, desc: 'integer given'
|
|
|
|
requires :name, type: String, desc: 'string given'
|
|
|
|
optional :email, type: String, desc: 'email given'
|
|
|
|
optional :others, type: Integer, values: [1, 2, 3]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
post '/type_given' do
|
|
|
|
{ 'declared_params' => declared(params) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# as body parameters it would be interpreted a bit different,
|
|
|
|
# cause it could not be distinguished anymore, so this would be translated to one array,
|
|
|
|
# see also next example for the difference
|
2024-05-10 18:42:54 +08:00
|
|
|
desc 'array_of_type' do
|
|
|
|
consumes ['application/x-www-form-urlencoded']
|
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
params do
|
2023-09-11 15:54:22 +08:00
|
|
|
requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'nested array of strings', example: %w[a b] }
|
2018-08-21 19:28:33 +08:00
|
|
|
requires :array_of_integer, type: Array[Integer], documentation: { param_type: 'body', desc: 'nested array of integers' }
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/array_of_type' do
|
|
|
|
{ 'declared_params' => declared(params) }
|
|
|
|
end
|
|
|
|
|
2024-05-10 18:42:54 +08:00
|
|
|
desc 'object_and_array' do
|
|
|
|
consumes ['application/x-www-form-urlencoded']
|
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
params do
|
|
|
|
requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'array of strings' }
|
|
|
|
requires :integer_value, type: Integer, documentation: { param_type: 'body', desc: 'integer value' }
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/object_and_array' do
|
|
|
|
{ 'declared_params' => declared(params) }
|
|
|
|
end
|
|
|
|
|
2024-05-10 18:42:54 +08:00
|
|
|
desc 'array_of_type_in_form' do
|
|
|
|
consumes ['application/x-www-form-urlencoded']
|
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
params do
|
|
|
|
requires :array_of_string, type: Array[String]
|
|
|
|
requires :array_of_integer, type: Array[Integer]
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/array_of_type_in_form' do
|
|
|
|
{ 'declared_params' => declared(params) }
|
|
|
|
end
|
|
|
|
|
2024-05-10 18:42:54 +08:00
|
|
|
desc 'array_of_entities' do
|
|
|
|
consumes ['application/x-www-form-urlencoded']
|
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
params do
|
|
|
|
requires :array_of_entities, type: Array[Entities::ApiError]
|
|
|
|
end
|
|
|
|
|
|
|
|
post '/array_of_entities' do
|
|
|
|
{ 'declared_params' => declared(params) }
|
|
|
|
end
|
|
|
|
|
|
|
|
add_swagger_documentation array_use_braces: array_use_braces
|
2015-11-04 20:03:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
describe 'retrieves the documentation for grouped parameters' do
|
|
|
|
subject do
|
|
|
|
get '/swagger_doc/groups'
|
|
|
|
JSON.parse(last_response.body)
|
2015-11-04 20:03:42 +08:00
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
specify do
|
|
|
|
expect(subject['paths']['/groups']['post']['parameters']).to eql(
|
|
|
|
[
|
|
|
|
{ 'in' => 'formData', 'name' => "required_group#{braces}[required_param_1]", 'required' => true, 'type' => 'array', 'items' => { 'type' => 'string' } },
|
|
|
|
{ 'in' => 'formData', 'name' => "required_group#{braces}[required_param_2]", 'required' => true, 'type' => 'array', 'items' => { 'type' => 'string' } }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2016-06-16 19:35:29 +08:00
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
describe 'retrieves the documentation for typed group parameters' do
|
|
|
|
subject do
|
|
|
|
get '/swagger_doc/type_given'
|
|
|
|
JSON.parse(last_response.body)
|
|
|
|
end
|
2016-07-07 23:44:07 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
specify do
|
|
|
|
expect(subject['paths']['/type_given']['post']['parameters']).to eql(
|
|
|
|
[
|
|
|
|
{ 'in' => 'formData', 'name' => "typed_group#{braces}[id]", 'description' => 'integer given', 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true },
|
|
|
|
{ 'in' => 'formData', 'name' => "typed_group#{braces}[name]", 'description' => 'string given', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
|
|
|
|
{ 'in' => 'formData', 'name' => "typed_group#{braces}[email]", 'description' => 'email given', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false },
|
|
|
|
{ 'in' => 'formData', 'name' => "typed_group#{braces}[others]", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32', 'enum' => [1, 2, 3] }, 'required' => false }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2016-07-07 23:44:07 +08:00
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
describe 'retrieves the documentation for parameters that are arrays of primitive types' do
|
|
|
|
subject do
|
|
|
|
get '/swagger_doc/array_of_type'
|
|
|
|
JSON.parse(last_response.body)
|
|
|
|
end
|
2016-06-16 19:35:29 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
specify do
|
2024-04-22 11:50:05 +08:00
|
|
|
expect(subject['definitions']['postArrayOfType']).to eql(
|
2018-08-21 19:28:33 +08:00
|
|
|
'type' => 'object',
|
2024-05-10 18:42:54 +08:00
|
|
|
'description' => 'array_of_type',
|
2018-08-21 19:28:33 +08:00
|
|
|
'properties' => {
|
|
|
|
'array_of_string' => {
|
2023-09-11 15:54:22 +08:00
|
|
|
'items' => { 'type' => 'string' }, 'type' => 'array', 'description' => 'nested array of strings', 'example' => %w[a b]
|
2018-08-21 19:28:33 +08:00
|
|
|
},
|
|
|
|
'array_of_integer' => {
|
2019-08-06 20:22:14 +08:00
|
|
|
'items' => { 'type' => 'integer', 'format' => 'int32' }, 'type' => 'array', 'description' => 'nested array of integers'
|
2018-08-21 19:28:33 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'required' => %w[array_of_string array_of_integer]
|
|
|
|
)
|
|
|
|
end
|
2016-06-16 19:35:29 +08:00
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
describe 'documentation for simple and array parameters' do
|
|
|
|
subject do
|
|
|
|
get '/swagger_doc/object_and_array'
|
|
|
|
JSON.parse(last_response.body)
|
|
|
|
end
|
2016-10-14 05:06:03 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
specify do
|
|
|
|
expect(subject['definitions']['postObjectAndArray']['type']).to eql 'object'
|
|
|
|
expect(subject['definitions']['postObjectAndArray']['properties']).to eql(
|
|
|
|
'array_of_string' => {
|
|
|
|
'type' => 'array',
|
|
|
|
'description' => 'array of strings',
|
|
|
|
'items' => {
|
|
|
|
'type' => 'string'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'integer_value' => {
|
|
|
|
'type' => 'integer', 'format' => 'int32', 'description' => 'integer value'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2016-10-14 05:06:03 +08:00
|
|
|
end
|
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
describe 'retrieves the documentation for typed group parameters' do
|
|
|
|
subject do
|
|
|
|
get '/swagger_doc/array_of_type_in_form'
|
|
|
|
JSON.parse(last_response.body)
|
|
|
|
end
|
2016-06-16 19:35:29 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
specify do
|
|
|
|
expect(subject['paths']['/array_of_type_in_form']['post']['parameters']).to eql(
|
|
|
|
[
|
|
|
|
{ 'in' => 'formData', 'name' => "array_of_string#{braces}", 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => true },
|
|
|
|
{ 'in' => 'formData', 'name' => "array_of_integer#{braces}", 'type' => 'array', 'items' => { 'type' => 'integer', 'format' => 'int32' }, 'required' => true }
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-10-14 05:06:03 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
describe 'documentation for entity array parameters' do
|
|
|
|
let(:parameters) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
'in' => 'formData',
|
|
|
|
'name' => "array_of_entities#{braces}",
|
|
|
|
'type' => 'array',
|
|
|
|
'items' => {
|
|
|
|
'$ref' => '#/definitions/ApiError'
|
|
|
|
},
|
|
|
|
'required' => true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
end
|
2016-10-14 05:06:03 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
subject do
|
|
|
|
get '/swagger_doc/array_of_entities'
|
|
|
|
JSON.parse(last_response.body)
|
|
|
|
end
|
2016-10-14 05:06:03 +08:00
|
|
|
|
2018-08-21 19:28:33 +08:00
|
|
|
specify do
|
|
|
|
expect(subject['definitions']['ApiError']).not_to be_blank
|
|
|
|
expect(subject['paths']['/array_of_entities']['post']['parameters']).to eql(parameters)
|
|
|
|
end
|
|
|
|
end
|
2016-10-14 05:06:03 +08:00
|
|
|
end
|
|
|
|
end
|
2015-11-04 20:03:42 +08:00
|
|
|
end
|