|
| 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 BodyParamTypeApi < Grape::API |
| 11 | + namespace :wo_entities do |
| 12 | + desc 'post in body /wo entity' |
| 13 | + params do |
| 14 | + requires :in_body_1, type: Integer, documentation: { desc: 'in_body_1', param_type: 'body' } |
| 15 | + optional :in_body_2, type: String, documentation: { desc: 'in_body_2', param_type: 'body' } |
| 16 | + optional :in_body_3, type: String, documentation: { desc: 'in_body_3', param_type: 'body' } |
| 17 | + end |
| 18 | + |
| 19 | + post '/in_body' do |
| 20 | + { 'declared_params' => declared(params) } |
| 21 | + end |
| 22 | + |
| 23 | + desc 'put in body /wo entity' |
| 24 | + params do |
| 25 | + requires :key, type: Integer |
| 26 | + optional :in_body_1, type: Integer, documentation: { desc: 'in_body_1', param_type: 'body' } |
| 27 | + optional :in_body_2, type: String, documentation: { desc: 'in_body_2', param_type: 'body' } |
| 28 | + optional :in_body_3, type: String, documentation: { desc: 'in_body_3', param_type: 'body' } |
| 29 | + end |
| 30 | + |
| 31 | + put '/in_body/:key' do |
| 32 | + { 'declared_params' => declared(params) } |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + namespace :with_entities do |
| 37 | + desc 'post in body with entity', |
| 38 | + success: ::Entities::ResponseItem |
| 39 | + params do |
| 40 | + requires :name, type: String, documentation: { desc: 'name', param_type: 'body' } |
| 41 | + end |
| 42 | + |
| 43 | + post '/in_body' do |
| 44 | + { 'declared_params' => declared(params) } |
| 45 | + end |
| 46 | + |
| 47 | + desc 'put in body with entity', |
| 48 | + success: ::Entities::ResponseItem |
| 49 | + params do |
| 50 | + requires :id, type: Integer |
| 51 | + optional :name, type: String, documentation: { desc: 'name', param_type: 'body' } |
| 52 | + end |
| 53 | + |
| 54 | + put '/in_body/:id' do |
| 55 | + { 'declared_params' => declared(params) } |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + namespace :with_entity_param do |
| 60 | + desc 'put in body with entity parameter' |
| 61 | + params do |
| 62 | + optional :data, type: ::Entities::NestedModule::ApiResponse, documentation: { desc: 'request data' } |
| 63 | + end |
| 64 | + |
| 65 | + post do |
| 66 | + { 'declared_params' => declared(params) } |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + add_swagger_documentation openapi_version: '3.0' |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def app |
| 76 | + TheApi::BodyParamTypeApi |
| 77 | + end |
| 78 | + |
| 79 | + describe 'no entity given' do |
| 80 | + subject do |
| 81 | + get '/swagger_doc/wo_entities' |
| 82 | + JSON.parse(last_response.body) |
| 83 | + end |
| 84 | + |
| 85 | + specify do |
| 86 | + expect(subject['paths']['/wo_entities/in_body']['post']['requestBody']['content']['application/json']).to eql( |
| 87 | + 'schema' => { |
| 88 | + 'properties' => { |
| 89 | + 'WoEntitiesInBody' => { '$ref' => '#/components/schemas/postWoEntitiesInBody' } |
| 90 | + }, |
| 91 | + 'required' => ['WoEntitiesInBody'], |
| 92 | + 'type' => 'object' |
| 93 | + } |
| 94 | + ) |
| 95 | + end |
| 96 | + |
| 97 | + specify do |
| 98 | + expect(subject['components']['schemas']['postWoEntitiesInBody']).to eql( |
| 99 | + 'description' => 'post in body /wo entity', |
| 100 | + 'type' => 'object', |
| 101 | + 'properties' => { |
| 102 | + 'in_body_1' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'in_body_1' }, |
| 103 | + 'in_body_2' => { 'type' => 'string', 'description' => 'in_body_2' }, |
| 104 | + 'in_body_3' => { 'type' => 'string', 'description' => 'in_body_3' } |
| 105 | + }, |
| 106 | + 'required' => ['in_body_1'] |
| 107 | + ) |
| 108 | + end |
| 109 | + |
| 110 | + specify do |
| 111 | + expect(subject['paths']['/wo_entities/in_body/{key}']['put']['parameters']).to eql( |
| 112 | + [ |
| 113 | + { 'in' => 'path', 'name' => 'key', 'schema' => { 'format' => 'int32', 'type' => 'integer' }, 'required' => true } |
| 114 | + ] |
| 115 | + ) |
| 116 | + |
| 117 | + expect(subject['paths']['/wo_entities/in_body/{key}']['put']['requestBody']['content']['application/json']).to eql( |
| 118 | + 'schema' => { |
| 119 | + 'properties' => { |
| 120 | + 'WoEntitiesInBody' => { '$ref' => '#/components/schemas/putWoEntitiesInBody' } |
| 121 | + }, |
| 122 | + 'required' => ['WoEntitiesInBody'], |
| 123 | + 'type' => 'object' |
| 124 | + } |
| 125 | + ) |
| 126 | + end |
| 127 | + |
| 128 | + specify do |
| 129 | + expect(subject['components']['schemas']['putWoEntitiesInBody']).to eql( |
| 130 | + 'description' => 'put in body /wo entity', |
| 131 | + 'type' => 'object', |
| 132 | + 'properties' => { |
| 133 | + 'in_body_1' => { 'type' => 'integer', 'format' => 'int32', 'description' => 'in_body_1' }, |
| 134 | + 'in_body_2' => { 'type' => 'string', 'description' => 'in_body_2' }, |
| 135 | + 'in_body_3' => { 'type' => 'string', 'description' => 'in_body_3' } |
| 136 | + } |
| 137 | + ) |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + describe 'entity given' do |
| 142 | + subject do |
| 143 | + get '/swagger_doc/with_entities' |
| 144 | + JSON.parse(last_response.body) |
| 145 | + end |
| 146 | + |
| 147 | + specify do |
| 148 | + expect(subject['paths']['/with_entities/in_body']['post']['requestBody']['content']['application/json']).to eql( |
| 149 | + 'schema' => { |
| 150 | + 'properties' => { |
| 151 | + 'WithEntitiesInBody' => { |
| 152 | + '$ref' => '#/components/schemas/postWithEntitiesInBody' |
| 153 | + } |
| 154 | + }, |
| 155 | + 'required' => ['WithEntitiesInBody'], |
| 156 | + 'type' => 'object' |
| 157 | + } |
| 158 | + ) |
| 159 | + end |
| 160 | + |
| 161 | + specify do |
| 162 | + expect(subject['components']['schemas']['postWithEntitiesInBody']).to eql( |
| 163 | + 'type' => 'object', |
| 164 | + 'properties' => { |
| 165 | + 'name' => { 'type' => 'string', 'description' => 'name' } |
| 166 | + }, |
| 167 | + 'required' => ['name'], |
| 168 | + 'description' => 'post in body with entity' |
| 169 | + ) |
| 170 | + end |
| 171 | + |
| 172 | + specify do |
| 173 | + expect(subject['paths']['/with_entities/in_body/{id}']['put']['parameters']).to eql( |
| 174 | + [ |
| 175 | + { |
| 176 | + 'in' => 'path', |
| 177 | + 'name' => 'id', |
| 178 | + 'schema' => { 'format' => 'int32', 'type' => 'integer' }, |
| 179 | + 'required' => true |
| 180 | + } |
| 181 | + ] |
| 182 | + ) |
| 183 | + |
| 184 | + expect(subject['paths']['/with_entities/in_body/{id}']['put']['requestBody']['content']['application/json']).to eql( |
| 185 | + 'schema' => { |
| 186 | + 'properties' => { |
| 187 | + 'WithEntitiesInBody' => { '$ref' => '#/components/schemas/putWithEntitiesInBody' } |
| 188 | + }, |
| 189 | + 'required' => ['WithEntitiesInBody'], |
| 190 | + 'type' => 'object' |
| 191 | + } |
| 192 | + ) |
| 193 | + end |
| 194 | + |
| 195 | + specify do |
| 196 | + expect(subject['components']['schemas']['putWithEntitiesInBody']).to eql( |
| 197 | + 'type' => 'object', |
| 198 | + 'properties' => { |
| 199 | + 'name' => { 'type' => 'string', 'description' => 'name' } |
| 200 | + }, |
| 201 | + 'description' => 'put in body with entity' |
| 202 | + ) |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + describe 'complex entity given' do |
| 207 | + let(:request_parameters_definition) do |
| 208 | + { |
| 209 | + 'schema' => { |
| 210 | + 'properties' => { |
| 211 | + 'WithEntityParam' => { '$ref' => '#/components/schemas/postWithEntityParam' } |
| 212 | + }, |
| 213 | + 'required' => ['WithEntityParam'], |
| 214 | + 'type' => 'object' |
| 215 | + } |
| 216 | + } |
| 217 | + end |
| 218 | + |
| 219 | + let(:request_body_parameters_definition) do |
| 220 | + { |
| 221 | + 'description' => 'put in body with entity parameter', |
| 222 | + 'properties' => { 'data' => { '$ref' => '#/components/schemas/ApiResponse', 'description' => 'request data' } }, |
| 223 | + 'type' => 'object' |
| 224 | + } |
| 225 | + end |
| 226 | + |
| 227 | + subject do |
| 228 | + get '/swagger_doc/with_entity_param' |
| 229 | + JSON.parse(last_response.body) |
| 230 | + end |
| 231 | + |
| 232 | + specify do |
| 233 | + expect(subject['paths']['/with_entity_param']['post']['requestBody']['content']['application/json']).to eql(request_parameters_definition) |
| 234 | + end |
| 235 | + |
| 236 | + specify do |
| 237 | + expect(subject['components']['schemas']['ApiResponse']).not_to be_nil |
| 238 | + end |
| 239 | + |
| 240 | + specify do |
| 241 | + expect(subject['components']['schemas']['postWithEntityParam']).to eql(request_body_parameters_definition) |
| 242 | + end |
| 243 | + end |
| 244 | +end |
0 commit comments