|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `header`' do |
| 6 | + include_context "#{MODEL_PARSER} swagger example" |
| 7 | + |
| 8 | + before :all do |
| 9 | + module TheApi |
| 10 | + class ParamTypeApi < Grape::API |
| 11 | + # using `:param_type` |
| 12 | + desc 'full set of request param types', |
| 13 | + success: Entities::UseResponse |
| 14 | + params do |
| 15 | + optional :in_query, type: String, documentation: { param_type: 'query' } |
| 16 | + optional :in_header, type: String, documentation: { param_type: 'header' } |
| 17 | + end |
| 18 | + |
| 19 | + get '/defined_param_type' do |
| 20 | + { 'declared_params' => declared(params) } |
| 21 | + end |
| 22 | + |
| 23 | + desc 'full set of request param types', |
| 24 | + success: Entities::UseResponse |
| 25 | + params do |
| 26 | + requires :in_path, type: Integer |
| 27 | + optional :in_query, type: String, documentation: { param_type: 'query' } |
| 28 | + optional :in_header, type: String, documentation: { param_type: 'header' } |
| 29 | + end |
| 30 | + |
| 31 | + get '/defined_param_type/:in_path' do |
| 32 | + { 'declared_params' => declared(params) } |
| 33 | + end |
| 34 | + |
| 35 | + desc 'full set of request param types', |
| 36 | + success: Entities::UseResponse |
| 37 | + params do |
| 38 | + optional :in_path, type: Integer |
| 39 | + optional :in_query, type: String, documentation: { param_type: 'query' } |
| 40 | + optional :in_header, type: String, documentation: { param_type: 'header' } |
| 41 | + end |
| 42 | + |
| 43 | + delete '/defined_param_type/:in_path' do |
| 44 | + { 'declared_params' => declared(params) } |
| 45 | + end |
| 46 | + |
| 47 | + # using `:in` |
| 48 | + desc 'full set of request param types using `:in`', |
| 49 | + success: Entities::UseResponse |
| 50 | + params do |
| 51 | + optional :in_query, type: String, documentation: { in: 'query' } |
| 52 | + optional :in_header, type: String, documentation: { in: 'header' } |
| 53 | + end |
| 54 | + |
| 55 | + get '/defined_in' do |
| 56 | + { 'declared_params' => declared(params) } |
| 57 | + end |
| 58 | + |
| 59 | + desc 'full set of request param types using `:in`', |
| 60 | + success: Entities::UseResponse |
| 61 | + params do |
| 62 | + requires :in_path, type: Integer |
| 63 | + optional :in_query, type: String, documentation: { in: 'query' } |
| 64 | + optional :in_header, type: String, documentation: { in: 'header' } |
| 65 | + end |
| 66 | + |
| 67 | + get '/defined_in/:in_path' do |
| 68 | + { 'declared_params' => declared(params) } |
| 69 | + end |
| 70 | + |
| 71 | + desc 'full set of request param types using `:in`' |
| 72 | + params do |
| 73 | + optional :in_path, type: Integer |
| 74 | + optional :in_query, type: String, documentation: { in: 'query' } |
| 75 | + optional :in_header, type: String, documentation: { in: 'header' } |
| 76 | + end |
| 77 | + |
| 78 | + delete '/defined_in/:in_path' do |
| 79 | + { 'declared_params' => declared(params) } |
| 80 | + end |
| 81 | + |
| 82 | + # file |
| 83 | + desc 'file download', |
| 84 | + success: Entities::UseResponse |
| 85 | + params do |
| 86 | + requires :name, type: String |
| 87 | + end |
| 88 | + |
| 89 | + get '/download' do |
| 90 | + { 'declared_params' => declared(params) } |
| 91 | + end |
| 92 | + |
| 93 | + desc 'file upload', |
| 94 | + success: Entities::UseResponse |
| 95 | + params do |
| 96 | + requires :name, type: File |
| 97 | + end |
| 98 | + |
| 99 | + post '/upload' do |
| 100 | + { 'declared_params' => declared(params) } |
| 101 | + end |
| 102 | + |
| 103 | + add_swagger_documentation openapi_version: '3.0' |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + def app |
| 109 | + TheApi::ParamTypeApi |
| 110 | + end |
| 111 | + |
| 112 | + describe 'foo' do |
| 113 | + subject do |
| 114 | + get '/swagger_doc' |
| 115 | + JSON.parse(last_response.body) |
| 116 | + end |
| 117 | + |
| 118 | + specify do |
| 119 | + expect(subject['paths']['/defined_param_type/{in_path}']['delete']['responses']).to eql( |
| 120 | + '200' => { |
| 121 | + 'content' => { |
| 122 | + 'application/json' => { |
| 123 | + 'schema' => { '$ref' => '#/components/schemas/UseResponse' } |
| 124 | + } |
| 125 | + }, |
| 126 | + 'description' => 'full set of request param types' |
| 127 | + } |
| 128 | + ) |
| 129 | + end |
| 130 | + |
| 131 | + specify do |
| 132 | + expect(subject['paths']['/defined_in/{in_path}']['delete']['responses']).to eql( |
| 133 | + '204' => { |
| 134 | + 'description' => 'full set of request param types using `:in`' |
| 135 | + } |
| 136 | + ) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe 'defined param types' do |
| 141 | + subject do |
| 142 | + get '/swagger_doc/defined_param_type' |
| 143 | + JSON.parse(last_response.body) |
| 144 | + end |
| 145 | + |
| 146 | + specify do |
| 147 | + expect(subject['paths']['/defined_param_type']['get']['parameters']).to eql( |
| 148 | + [ |
| 149 | + { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'schema' => { 'type' => 'string' } }, |
| 150 | + { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'schema' => { 'type' => 'string' } } |
| 151 | + ] |
| 152 | + ) |
| 153 | + end |
| 154 | + |
| 155 | + specify do |
| 156 | + expect(subject['paths']['/defined_param_type/{in_path}']['get']['parameters']).to eql( |
| 157 | + [ |
| 158 | + { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'schema' => { 'type' => 'integer', 'format' => 'int32' } }, |
| 159 | + { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'schema' => { 'type' => 'string' } }, |
| 160 | + { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'schema' => { 'type' => 'string' } } |
| 161 | + ] |
| 162 | + ) |
| 163 | + end |
| 164 | + |
| 165 | + specify do |
| 166 | + expect(subject['paths']['/defined_param_type/{in_path}']['delete']['parameters']).to eql( |
| 167 | + [ |
| 168 | + { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'schema' => { 'type' => 'integer', 'format' => 'int32' } }, |
| 169 | + { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'schema' => { 'type' => 'string' } }, |
| 170 | + { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'schema' => { 'type' => 'string' } } |
| 171 | + ] |
| 172 | + ) |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + describe 'defined param types with `:in`' do |
| 177 | + subject do |
| 178 | + get '/swagger_doc/defined_in' |
| 179 | + JSON.parse(last_response.body) |
| 180 | + end |
| 181 | + |
| 182 | + specify do |
| 183 | + expect(subject['paths']['/defined_in']['get']['parameters']).to eql( |
| 184 | + [ |
| 185 | + { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'schema' => { 'type' => 'string' } }, |
| 186 | + { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'schema' => { 'type' => 'string' } } |
| 187 | + ] |
| 188 | + ) |
| 189 | + end |
| 190 | + |
| 191 | + specify do |
| 192 | + expect(subject['paths']['/defined_in/{in_path}']['get']['parameters']).to eql( |
| 193 | + [ |
| 194 | + { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'schema' => { 'type' => 'integer', 'format' => 'int32' } }, |
| 195 | + { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'schema' => { 'type' => 'string' } }, |
| 196 | + { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'schema' => { 'type' => 'string' } } |
| 197 | + ] |
| 198 | + ) |
| 199 | + end |
| 200 | + |
| 201 | + specify do |
| 202 | + expect(subject['paths']['/defined_in/{in_path}']['delete']['parameters']).to eql( |
| 203 | + [ |
| 204 | + { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'schema' => { 'type' => 'integer', 'format' => 'int32' } }, |
| 205 | + { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'schema' => { 'type' => 'string' } }, |
| 206 | + { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'schema' => { 'type' => 'string' } } |
| 207 | + ] |
| 208 | + ) |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + describe 'file' do |
| 213 | + describe 'upload' do |
| 214 | + subject do |
| 215 | + get '/swagger_doc/upload' |
| 216 | + JSON.parse(last_response.body) |
| 217 | + end |
| 218 | + |
| 219 | + specify do |
| 220 | + expect(subject['paths']['/upload']['post']['requestBody']).to eql( |
| 221 | + 'content' => { |
| 222 | + 'application/json' => { 'schema' => { 'properties' => {}, 'type' => 'object' } }, |
| 223 | + 'application/octet-stream' => { |
| 224 | + 'schema' => { |
| 225 | + 'properties' => { 'name' => { 'format' => 'binary', 'type' => 'string' } }, |
| 226 | + 'required' => ['name'], |
| 227 | + 'type' => 'object' |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + ) |
| 232 | + end |
| 233 | + end |
| 234 | + |
| 235 | + describe 'download' do |
| 236 | + subject do |
| 237 | + get '/swagger_doc/download' |
| 238 | + JSON.parse(last_response.body) |
| 239 | + end |
| 240 | + |
| 241 | + specify do |
| 242 | + expect(subject['paths']['/download']['get']['parameters']).to eql( |
| 243 | + [ |
| 244 | + { 'in' => 'query', 'name' => 'name', 'required' => true, 'schema' => { 'type' => 'string' } } |
| 245 | + ] |
| 246 | + ) |
| 247 | + end |
| 248 | + end |
| 249 | + end |
| 250 | +end |
0 commit comments