| 
									
										
										
										
											2017-03-08 16:32:31 +08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2017-03-28 17:15:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  | require 'spec_helper' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-24 07:30:38 +08:00
										 |  |  | describe 'response' do | 
					
						
							| 
									
										
										
										
											2016-05-07 09:12:26 +08:00
										 |  |  |   include_context "#{MODEL_PARSER} swagger example" | 
					
						
							| 
									
										
										
										
											2015-12-18 05:12:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |   before :all do | 
					
						
							|  |  |  |     module TheApi | 
					
						
							|  |  |  |       class ResponseApi < Grape::API | 
					
						
							|  |  |  |         format :json | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         desc 'This returns something', | 
					
						
							| 
									
										
										
										
											2024-05-10 18:42:54 +08:00
										 |  |  |              consumes: ['application/x-www-form-urlencoded'], | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |              params: Entities::UseResponse.documentation, | 
					
						
							|  |  |  |              failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] | 
					
						
							| 
									
										
										
										
											2016-07-04 23:42:28 +08:00
										 |  |  |         post '/params_given' do | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |           { 'declared_params' => declared(params) } | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         desc 'This returns something', | 
					
						
							| 
									
										
										
										
											2024-05-10 18:42:54 +08:00
										 |  |  |              consumes: ['application/x-www-form-urlencoded'], | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |              entity: Entities::UseResponse, | 
					
						
							|  |  |  |              failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |         get '/entity_response' do | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |           { 'declared_params' => declared(params) } | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-16 19:30:52 +08:00
										 |  |  |         desc 'This returns something', | 
					
						
							| 
									
										
										
										
											2024-05-10 18:42:54 +08:00
										 |  |  |              consumes: ['application/x-www-form-urlencoded'], | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |              entity: Entities::UseItemResponseAsType, | 
					
						
							|  |  |  |              failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] | 
					
						
							| 
									
										
										
										
											2016-03-16 19:30:52 +08:00
										 |  |  |         get '/nested_type' do | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |           { 'declared_params' => declared(params) } | 
					
						
							| 
									
										
										
										
											2016-03-16 19:30:52 +08:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-20 01:39:40 +08:00
										 |  |  |         desc 'This returns something', | 
					
						
							| 
									
										
										
										
											2024-05-10 18:42:54 +08:00
										 |  |  |              consumes: ['application/x-www-form-urlencoded'], | 
					
						
							| 
									
										
										
										
											2019-06-20 01:39:40 +08:00
										 |  |  |              success: [ | 
					
						
							|  |  |  |                { code: 200, message: 'Request has succeeded' }, | 
					
						
							|  |  |  |                { code: 201, message: 'Successful Operation' }, | 
					
						
							|  |  |  |                { code: 204, message: 'Request was fulfilled' } | 
					
						
							|  |  |  |              ], | 
					
						
							|  |  |  |              failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }] | 
					
						
							|  |  |  |         get '/multiple-success-responses' do | 
					
						
							|  |  |  |           { 'declared_params' => declared(params) } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |         add_swagger_documentation | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def app | 
					
						
							|  |  |  |     TheApi::ResponseApi | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |   describe 'uses nested type as response object' do | 
					
						
							| 
									
										
										
										
											2016-03-16 19:30:52 +08:00
										 |  |  |     subject do | 
					
						
							|  |  |  |       get '/swagger_doc/nested_type' | 
					
						
							|  |  |  |       JSON.parse(last_response.body) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     specify do | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |       expect(subject['paths']['/nested_type']['get']).to eql( | 
					
						
							|  |  |  |         'description' => 'This returns something', | 
					
						
							|  |  |  |         'produces' => ['application/json'], | 
					
						
							|  |  |  |         'responses' => { | 
					
						
							|  |  |  |           '200' => { 'description' => 'This returns something', 'schema' => { '$ref' => '#/definitions/UseItemResponseAsType' } }, | 
					
						
							|  |  |  |           '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } } | 
					
						
							| 
									
										
										
										
											2016-05-01 18:31:44 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |         'tags' => ['nested_type'], | 
					
						
							| 
									
										
										
										
											2016-05-11 04:01:13 +08:00
										 |  |  |         'operationId' => 'getNestedType' | 
					
						
							|  |  |  |       ) | 
					
						
							| 
									
										
										
										
											2016-05-07 09:12:26 +08:00
										 |  |  |       expect(subject['definitions']).to eql(swagger_nested_type) | 
					
						
							| 
									
										
										
										
											2016-03-16 19:30:52 +08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |   describe 'uses entity as response object' do | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |     subject do | 
					
						
							|  |  |  |       get '/swagger_doc/entity_response' | 
					
						
							|  |  |  |       JSON.parse(last_response.body) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-19 01:40:53 +08:00
										 |  |  |     specify do | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |       expect(subject['paths']['/entity_response']['get']).to eql( | 
					
						
							|  |  |  |         'description' => 'This returns something', | 
					
						
							|  |  |  |         'produces' => ['application/json'], | 
					
						
							|  |  |  |         'responses' => { | 
					
						
							|  |  |  |           '200' => { 'description' => 'This returns something', 'schema' => { '$ref' => '#/definitions/UseResponse' } }, | 
					
						
							|  |  |  |           '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } } | 
					
						
							| 
									
										
										
										
											2016-05-01 18:31:44 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |         'tags' => ['entity_response'], | 
					
						
							| 
									
										
										
										
											2016-05-11 04:01:13 +08:00
										 |  |  |         'operationId' => 'getEntityResponse' | 
					
						
							|  |  |  |       ) | 
					
						
							| 
									
										
										
										
											2016-05-07 09:12:26 +08:00
										 |  |  |       expect(subject['definitions']).to eql(swagger_entity_as_response_object) | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |   describe 'uses params as response object' do | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |     subject do | 
					
						
							| 
									
										
										
										
											2016-07-04 23:42:28 +08:00
										 |  |  |       get '/swagger_doc/params_given' | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |       JSON.parse(last_response.body) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-19 01:40:53 +08:00
										 |  |  |     specify do | 
					
						
							| 
									
										
										
										
											2016-07-04 23:42:28 +08:00
										 |  |  |       expect(subject['paths']['/params_given']['post']).to eql( | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |         'description' => 'This returns something', | 
					
						
							|  |  |  |         'produces' => ['application/json'], | 
					
						
							| 
									
										
										
										
											2024-05-10 18:42:54 +08:00
										 |  |  |         'consumes' => ['application/x-www-form-urlencoded'], | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |         'parameters' => [ | 
					
						
							|  |  |  |           { 'in' => 'formData', 'name' => 'description', 'type' => 'string', 'required' => false }, | 
					
						
							|  |  |  |           { 'in' => 'formData', 'name' => '$responses', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false } | 
					
						
							| 
									
										
										
										
											2016-03-16 19:30:52 +08:00
										 |  |  |         ], | 
					
						
							| 
									
										
										
										
											2016-05-07 03:00:36 +08:00
										 |  |  |         'responses' => { | 
					
						
							|  |  |  |           '201' => { 'description' => 'This returns something' }, | 
					
						
							|  |  |  |           '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } } | 
					
						
							| 
									
										
										
										
											2016-05-01 18:31:44 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2016-07-04 23:42:28 +08:00
										 |  |  |         'tags' => ['params_given'], | 
					
						
							|  |  |  |         'operationId' => 'postParamsGiven' | 
					
						
							| 
									
										
										
										
											2016-05-11 04:01:13 +08:00
										 |  |  |       ) | 
					
						
							| 
									
										
										
										
											2016-05-07 09:12:26 +08:00
										 |  |  |       expect(subject['definitions']).to eql(swagger_params_as_response_object) | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2019-06-20 01:39:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe 'uses params as response object when response contains multiple values for success' do | 
					
						
							|  |  |  |     subject do | 
					
						
							|  |  |  |       get '/swagger_doc/multiple-success-responses' | 
					
						
							|  |  |  |       JSON.parse(last_response.body) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     specify do | 
					
						
							|  |  |  |       expect(subject['paths']['/multiple-success-responses']['get']).to eql( | 
					
						
							|  |  |  |         'description' => 'This returns something', | 
					
						
							|  |  |  |         'produces' => ['application/json'], | 
					
						
							|  |  |  |         'responses' => { | 
					
						
							|  |  |  |           '200' => { 'description' => 'Request has succeeded' }, | 
					
						
							|  |  |  |           '201' => { 'description' => 'Successful Operation' }, | 
					
						
							|  |  |  |           '204' => { 'description' => 'Request was fulfilled' }, | 
					
						
							|  |  |  |           '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'tags' => ['multiple-success-responses'], | 
					
						
							|  |  |  |         'operationId' => 'getMultipleSuccessResponses' | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |       expect(subject['definitions']).to eql(swagger_params_as_response_object) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  | end |