|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe 'response with examples' do |
| 6 | + include_context "#{MODEL_PARSER} swagger example" |
| 7 | + |
| 8 | + before :all do |
| 9 | + module TheApi |
| 10 | + class ResponseApiExamples < Grape::API |
| 11 | + format :json |
| 12 | + |
| 13 | + desc 'This returns examples' do |
| 14 | + success model: Entities::UseResponse, examples: { 'application/json' => { description: 'Names list', items: [{ id: '123', name: 'John' }] } } |
| 15 | + failure [[404, 'NotFound', Entities::ApiError, { 'application/json' => { code: 404, message: 'Not found' } }]] |
| 16 | + end |
| 17 | + get '/response_examples' do |
| 18 | + { 'declared_params' => declared(params) } |
| 19 | + end |
| 20 | + |
| 21 | + desc 'This returns multiple examples' do |
| 22 | + success model: Entities::UseResponse, examples: { 'foo' => { description: 'Names list', items: [{ id: '123', name: 'John' }] }, 'bar' => { description: 'Another list', items: [{ id: '123', something: 'John' }] } } |
| 23 | + failure [[404, 'NotFound', Entities::ApiError, { 'application/json' => { code: 404, message: 'Not found' } }]] |
| 24 | + end |
| 25 | + get '/response_multiple_examples' do |
| 26 | + { 'declared_params' => declared(params) } |
| 27 | + end |
| 28 | + |
| 29 | + desc 'This syntax also returns examples' do |
| 30 | + success model: Entities::UseResponse, examples: { 'application/json' => { description: 'Names list', items: [{ id: '123', name: 'John' }] } } |
| 31 | + failure [ |
| 32 | + { |
| 33 | + code: 404, |
| 34 | + message: 'NotFound', |
| 35 | + model: Entities::ApiError, |
| 36 | + examples: { 'application/json' => { code: 404, message: 'Not found' } } |
| 37 | + }, |
| 38 | + { |
| 39 | + code: 400, |
| 40 | + message: 'BadRequest', |
| 41 | + model: Entities::ApiError, |
| 42 | + examples: { 'application/json' => { code: 400, message: 'Bad Request' } } |
| 43 | + } |
| 44 | + ] |
| 45 | + end |
| 46 | + get '/response_failure_examples' do |
| 47 | + { 'declared_params' => declared(params) } |
| 48 | + end |
| 49 | + |
| 50 | + desc 'This does not return examples' do |
| 51 | + success model: Entities::UseResponse |
| 52 | + failure [[404, 'NotFound', Entities::ApiError]] |
| 53 | + end |
| 54 | + get '/response_no_examples' do |
| 55 | + { 'declared_params' => declared(params) } |
| 56 | + end |
| 57 | + add_swagger_documentation openapi_version: '3.0' |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def app |
| 63 | + TheApi::ResponseApiExamples |
| 64 | + end |
| 65 | + |
| 66 | + describe 'response examples' do |
| 67 | + let(:example_200) { { 'description' => 'Names list', 'items' => [{ 'id' => '123', 'name' => 'John' }] } } |
| 68 | + let(:example_404) { { 'code' => 404, 'message' => 'Not found' } } |
| 69 | + |
| 70 | + subject do |
| 71 | + get '/swagger_doc/response_examples' |
| 72 | + JSON.parse(last_response.body) |
| 73 | + end |
| 74 | + |
| 75 | + specify do |
| 76 | + expect(subject['paths']['/response_examples']['get']).to eql( |
| 77 | + 'description' => 'This returns examples', |
| 78 | + 'responses' => { |
| 79 | + '200' => { |
| 80 | + 'content' => { |
| 81 | + 'application/json' => { |
| 82 | + 'example' => example_200, |
| 83 | + 'schema' => { '$ref' => '#/components/schemas/UseResponse' } |
| 84 | + } |
| 85 | + }, |
| 86 | + 'description' => 'This returns examples' |
| 87 | + }, |
| 88 | + '404' => { |
| 89 | + 'content' => { |
| 90 | + 'application/json' => { |
| 91 | + 'example' => example_404, |
| 92 | + 'schema' => { '$ref' => '#/components/schemas/ApiError' } |
| 93 | + } |
| 94 | + }, |
| 95 | + 'description' => 'NotFound' |
| 96 | + } |
| 97 | + }, |
| 98 | + 'tags' => ['response_examples'], |
| 99 | + 'operationId' => 'getResponseExamples' |
| 100 | + ) |
| 101 | + end |
| 102 | + end |
| 103 | + |
| 104 | + describe 'response multiple examples' do |
| 105 | + let(:example_200) do |
| 106 | + { |
| 107 | + 'bar' => { 'value' => { 'description' => 'Another list', 'items' => [{ 'id' => '123', 'something' => 'John' }] } }, |
| 108 | + 'foo' => { 'value' => { 'description' => 'Names list', 'items' => [{ 'id' => '123', 'name' => 'John' }] } } |
| 109 | + } |
| 110 | + end |
| 111 | + let(:example_404) { { 'code' => 404, 'message' => 'Not found' } } |
| 112 | + |
| 113 | + subject do |
| 114 | + get '/swagger_doc/response_multiple_examples' |
| 115 | + JSON.parse(last_response.body) |
| 116 | + end |
| 117 | + |
| 118 | + specify do |
| 119 | + expect(subject['paths']['/response_multiple_examples']['get']).to eql( |
| 120 | + 'description' => 'This returns multiple examples', |
| 121 | + 'responses' => { |
| 122 | + '200' => { |
| 123 | + 'content' => { |
| 124 | + 'application/json' => { |
| 125 | + 'examples' => example_200, |
| 126 | + 'schema' => { '$ref' => '#/components/schemas/UseResponse' } |
| 127 | + } |
| 128 | + }, |
| 129 | + 'description' => 'This returns multiple examples' |
| 130 | + }, |
| 131 | + '404' => { |
| 132 | + 'content' => { |
| 133 | + 'application/json' => { |
| 134 | + 'example' => example_404, |
| 135 | + 'schema' => { '$ref' => '#/components/schemas/ApiError' } |
| 136 | + } |
| 137 | + }, |
| 138 | + 'description' => 'NotFound' |
| 139 | + } |
| 140 | + }, |
| 141 | + 'tags' => ['response_multiple_examples'], |
| 142 | + 'operationId' => 'getResponseMultipleExamples' |
| 143 | + ) |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + describe 'response failure examples' do |
| 148 | + let(:example_200) do |
| 149 | + { 'application/json' => { 'description' => 'Names list', 'items' => [{ 'id' => '123', 'name' => 'John' }] } } |
| 150 | + end |
| 151 | + let(:example_404) do |
| 152 | + { 'application/json' => { 'code' => 404, 'message' => 'Not found' } } |
| 153 | + end |
| 154 | + let(:example_400) do |
| 155 | + { 'application/json' => { 'code' => 400, 'message' => 'Bad Request' } } |
| 156 | + end |
| 157 | + |
| 158 | + subject do |
| 159 | + get '/swagger_doc/response_failure_examples' |
| 160 | + puts last_response.body |
| 161 | + JSON.parse(last_response.body) |
| 162 | + end |
| 163 | + |
| 164 | + specify do |
| 165 | + expect(subject['paths']['/response_failure_examples']['get']).to eql( |
| 166 | + 'description' => 'This syntax also returns examples', |
| 167 | + 'responses' => { |
| 168 | + '200' => { |
| 169 | + 'content' => { |
| 170 | + 'application/json' => { |
| 171 | + 'example' => { 'description' => 'Names list', 'items' => [{ 'id' => '123', 'name' => 'John' }] }, |
| 172 | + 'schema' => { '$ref' => '#/components/schemas/UseResponse' } |
| 173 | + } |
| 174 | + }, |
| 175 | + 'description' => 'This syntax also returns examples' |
| 176 | + }, |
| 177 | + '400' => { |
| 178 | + 'content' => { |
| 179 | + 'application/json' => { |
| 180 | + 'example' => { 'code' => 400, 'message' => 'Bad Request' }, |
| 181 | + 'schema' => { '$ref' => '#/components/schemas/ApiError' } |
| 182 | + } |
| 183 | + }, |
| 184 | + 'description' => 'BadRequest' |
| 185 | + }, |
| 186 | + '404' => { |
| 187 | + 'content' => { |
| 188 | + 'application/json' => { |
| 189 | + 'example' => { 'code' => 404, 'message' => 'Not found' }, |
| 190 | + 'schema' => { '$ref' => '#/components/schemas/ApiError' } |
| 191 | + } |
| 192 | + }, |
| 193 | + 'description' => 'NotFound' |
| 194 | + } |
| 195 | + }, |
| 196 | + 'tags' => ['response_failure_examples'], |
| 197 | + 'operationId' => 'getResponseFailureExamples' |
| 198 | + ) |
| 199 | + end |
| 200 | + end |
| 201 | + |
| 202 | + describe 'response no examples' do |
| 203 | + subject do |
| 204 | + get '/swagger_doc/response_no_examples' |
| 205 | + JSON.parse(last_response.body) |
| 206 | + end |
| 207 | + |
| 208 | + specify do |
| 209 | + expect(subject['paths']['/response_no_examples']['get']).to eql( |
| 210 | + 'description' => 'This does not return examples', |
| 211 | + 'responses' => { |
| 212 | + '200' => { |
| 213 | + 'content' => { |
| 214 | + 'application/json' => { 'schema' => { '$ref' => '#/components/schemas/UseResponse' } } |
| 215 | + }, |
| 216 | + 'description' => 'This does not return examples' |
| 217 | + }, |
| 218 | + '404' => { |
| 219 | + 'content' => { |
| 220 | + 'application/json' => { 'schema' => { '$ref' => '#/components/schemas/ApiError' } } |
| 221 | + }, |
| 222 | + 'description' => 'NotFound' |
| 223 | + } |
| 224 | + }, |
| 225 | + 'tags' => ['response_no_examples'], |
| 226 | + 'operationId' => 'getResponseNoExamples' |
| 227 | + ) |
| 228 | + end |
| 229 | + end |
| 230 | +end |
0 commit comments