|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'Group Params as Array' do |
| 6 | + include_context "#{MODEL_PARSER} swagger example" |
| 7 | + |
| 8 | + [true, false].each do |array_use_braces| |
| 9 | + context "when array_use_braces option is set to #{array_use_braces}" do |
| 10 | + let(:braces) { array_use_braces ? '[]' : '' } |
| 11 | + |
| 12 | + let(:app) do |
| 13 | + Class.new(Grape::API) do |
| 14 | + format :json |
| 15 | + |
| 16 | + params do |
| 17 | + requires :required_group, type: Array do |
| 18 | + requires :required_param_1 |
| 19 | + requires :required_param_2 |
| 20 | + end |
| 21 | + end |
| 22 | + post '/groups' do |
| 23 | + { 'declared_params' => declared(params) } |
| 24 | + end |
| 25 | + |
| 26 | + params do |
| 27 | + requires :typed_group, type: Array do |
| 28 | + requires :id, type: Integer, desc: 'integer given' |
| 29 | + requires :name, type: String, desc: 'string given' |
| 30 | + optional :email, type: String, desc: 'email given' |
| 31 | + optional :others, type: Integer, values: [1, 2, 3] |
| 32 | + end |
| 33 | + end |
| 34 | + post '/type_given' do |
| 35 | + { 'declared_params' => declared(params) } |
| 36 | + end |
| 37 | + |
| 38 | + # as body parameters it would be interpreted a bit different, |
| 39 | + # cause it could not be distinguished anymore, so this would be translated to one array, |
| 40 | + # see also next example for the difference |
| 41 | + params do |
| 42 | + requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'nested array of strings' } |
| 43 | + requires :array_of_integer, type: Array[Integer], documentation: { param_type: 'body', desc: 'nested array of integers' } |
| 44 | + end |
| 45 | + |
| 46 | + post '/array_of_type' do |
| 47 | + { 'declared_params' => declared(params) } |
| 48 | + end |
| 49 | + |
| 50 | + params do |
| 51 | + requires :array_of_string, type: Array[String], documentation: { param_type: 'body', desc: 'array of strings' } |
| 52 | + requires :integer_value, type: Integer, documentation: { param_type: 'body', desc: 'integer value' } |
| 53 | + end |
| 54 | + |
| 55 | + post '/object_and_array' do |
| 56 | + { 'declared_params' => declared(params) } |
| 57 | + end |
| 58 | + |
| 59 | + params do |
| 60 | + requires :array_of_string, type: Array[String] |
| 61 | + requires :array_of_integer, type: Array[Integer] |
| 62 | + end |
| 63 | + |
| 64 | + post '/array_of_type_in_form' do |
| 65 | + { 'declared_params' => declared(params) } |
| 66 | + end |
| 67 | + |
| 68 | + params do |
| 69 | + requires :array_of_entities, type: Array[Entities::ApiError] |
| 70 | + end |
| 71 | + |
| 72 | + post '/array_of_entities' do |
| 73 | + { 'declared_params' => declared(params) } |
| 74 | + end |
| 75 | + |
| 76 | + add_swagger_documentation openapi_version: '3.0', array_use_braces: array_use_braces |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + describe 'retrieves the documentation for grouped parameters' do |
| 81 | + subject do |
| 82 | + get '/swagger_doc/groups' |
| 83 | + puts last_response.body |
| 84 | + JSON.parse(last_response.body) |
| 85 | + end |
| 86 | + |
| 87 | + specify do |
| 88 | + expect(subject['paths']['/groups']['post']['requestBody']['content']['application/x-www-form-urlencoded']).to eql( |
| 89 | + 'schema' => { |
| 90 | + 'properties' => { |
| 91 | + "required_group#{braces}[required_param_1]" => { 'items' => { 'type' => 'string' }, 'type' => 'array' }, |
| 92 | + "required_group#{braces}[required_param_2]" => { 'items' => { 'type' => 'string' }, 'type' => 'array' } |
| 93 | + }, |
| 94 | + 'required' => %W[required_group#{braces}[required_param_1] required_group#{braces}[required_param_2]], |
| 95 | + 'type' => 'object' |
| 96 | + } |
| 97 | + ) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + describe 'retrieves the documentation for typed group parameters' do |
| 102 | + subject do |
| 103 | + get '/swagger_doc/type_given' |
| 104 | + puts last_response.body |
| 105 | + JSON.parse(last_response.body) |
| 106 | + end |
| 107 | + |
| 108 | + specify do |
| 109 | + expect(subject['paths']['/type_given']['post']['requestBody']['content']['application/x-www-form-urlencoded']).to eql( |
| 110 | + 'schema' => { |
| 111 | + 'properties' => { |
| 112 | + "typed_group#{braces}[email]" => { |
| 113 | + 'description' => 'email given', |
| 114 | + 'items' => { 'type' => 'string' }, |
| 115 | + 'type' => 'array' |
| 116 | + }, |
| 117 | + "typed_group#{braces}[id]" => { |
| 118 | + 'description' => 'integer given', |
| 119 | + 'format' => 'int32', |
| 120 | + 'items' => { 'type' => 'integer' }, |
| 121 | + 'type' => 'array' |
| 122 | + }, |
| 123 | + "typed_group#{braces}[name]" => { |
| 124 | + 'description' => 'string given', |
| 125 | + 'items' => { 'type' => 'string' }, |
| 126 | + 'type' => 'array' |
| 127 | + }, |
| 128 | + "typed_group#{braces}[others]" => { |
| 129 | + 'format' => 'int32', |
| 130 | + 'items' => { 'enum' => [1, 2, 3], 'type' => 'integer' }, |
| 131 | + 'type' => 'array' |
| 132 | + } |
| 133 | + }, |
| 134 | + 'required' => %W[typed_group#{braces}[id] typed_group#{braces}[name]], 'type' => 'object' |
| 135 | + } |
| 136 | + ) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe 'retrieves the documentation for parameters that are arrays of primitive types' do |
| 141 | + subject do |
| 142 | + get '/swagger_doc/array_of_type' |
| 143 | + JSON.parse(last_response.body) |
| 144 | + end |
| 145 | + |
| 146 | + specify do |
| 147 | + expect(subject['components']['schemas']['postArrayOfType']['type']).to eql 'array' |
| 148 | + expect(subject['components']['schemas']['postArrayOfType']['items']).to eql( |
| 149 | + 'type' => 'object', |
| 150 | + 'properties' => { |
| 151 | + 'array_of_string' => { |
| 152 | + 'type' => 'string', 'description' => 'nested array of strings' |
| 153 | + }, |
| 154 | + 'array_of_integer' => { |
| 155 | + 'type' => 'integer', 'description' => 'nested array of integers' |
| 156 | + } |
| 157 | + }, |
| 158 | + 'required' => %w[array_of_string array_of_integer] |
| 159 | + ) |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + describe 'documentation for simple and array parameters' do |
| 164 | + subject do |
| 165 | + get '/swagger_doc/object_and_array' |
| 166 | + puts last_response.body |
| 167 | + JSON.parse(last_response.body) |
| 168 | + end |
| 169 | + |
| 170 | + specify do |
| 171 | + expect(subject['definitions']['postObjectAndArray']['type']).to eql 'object' |
| 172 | + expect(subject['definitions']['postObjectAndArray']['properties']).to eql( |
| 173 | + 'array_of_string' => { |
| 174 | + 'type' => 'array', |
| 175 | + 'description' => 'array of strings', |
| 176 | + 'items' => { |
| 177 | + 'type' => 'string' |
| 178 | + } |
| 179 | + }, |
| 180 | + 'integer_value' => { |
| 181 | + 'type' => 'integer', 'format' => 'int32', 'description' => 'integer value' |
| 182 | + } |
| 183 | + ) |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + describe 'retrieves the documentation for typed group parameters' do |
| 188 | + subject do |
| 189 | + get '/swagger_doc/array_of_type_in_form' |
| 190 | + JSON.parse(last_response.body) |
| 191 | + end |
| 192 | + |
| 193 | + specify do |
| 194 | + expect(subject['paths']['/array_of_type_in_form']['post']['requestBody']['content']['application/x-www-form-urlencoded']).to eql( |
| 195 | + 'schema' => { |
| 196 | + 'properties' => { |
| 197 | + "array_of_integer#{braces}" => { 'format' => 'int32', 'items' => { 'type' => 'integer' }, 'type' => 'array' }, |
| 198 | + "array_of_string#{braces}" => { 'items' => { 'type' => 'string' }, 'type' => 'array' } |
| 199 | + }, |
| 200 | + 'required' => %W[array_of_string#{braces} array_of_integer#{braces}], |
| 201 | + 'type' => 'object' |
| 202 | + } |
| 203 | + ) |
| 204 | + end |
| 205 | + end |
| 206 | + |
| 207 | + describe 'documentation for entity array parameters' do |
| 208 | + let(:parameters) do |
| 209 | + { |
| 210 | + 'properties' => { |
| 211 | + "array_of_entities#{braces}" => { |
| 212 | + 'items' => { '$ref' => '#/components/schemas/ApiError' }, |
| 213 | + 'type' => 'array' |
| 214 | + } |
| 215 | + }, |
| 216 | + 'required' => ["array_of_entities#{braces}"], 'type' => 'object' |
| 217 | + } |
| 218 | + end |
| 219 | + |
| 220 | + subject do |
| 221 | + get '/swagger_doc/array_of_entities' |
| 222 | + JSON.parse(last_response.body) |
| 223 | + end |
| 224 | + |
| 225 | + specify do |
| 226 | + expect(subject['components']['schemas']['ApiError']).not_to be_blank |
| 227 | + expect(subject['paths']['/array_of_entities']['post']['requestBody']['content']['application/x-www-form-urlencoded']['schema']).to eql(parameters) |
| 228 | + end |
| 229 | + end |
| 230 | + end |
| 231 | + end |
| 232 | +end |
0 commit comments