gitlab-grape-swagger 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.github/dependabot.yml +20 -0
  4. data/.github/workflows/ci.yml +45 -0
  5. data/.gitignore +44 -0
  6. data/.gitlab-ci.yml +19 -0
  7. data/.rspec +3 -0
  8. data/.rubocop.yml +136 -0
  9. data/.rubocop_todo.yml +60 -0
  10. data/.ruby-gemset +1 -0
  11. data/CHANGELOG.md +671 -0
  12. data/CONTRIBUTING.md +126 -0
  13. data/Dangerfile +3 -0
  14. data/Gemfile +45 -0
  15. data/Gemfile.lock +249 -0
  16. data/LICENSE.txt +20 -0
  17. data/README.md +1772 -0
  18. data/RELEASING.md +82 -0
  19. data/Rakefile +20 -0
  20. data/UPGRADING.md +201 -0
  21. data/example/api/endpoints.rb +131 -0
  22. data/example/api/entities.rb +18 -0
  23. data/example/config.ru +42 -0
  24. data/example/example_requests.postman_collection +146 -0
  25. data/example/splines.png +0 -0
  26. data/example/swagger-example.png +0 -0
  27. data/grape-swagger.gemspec +23 -0
  28. data/lib/grape-swagger/doc_methods/build_model_definition.rb +68 -0
  29. data/lib/grape-swagger/doc_methods/data_type.rb +110 -0
  30. data/lib/grape-swagger/doc_methods/extensions.rb +101 -0
  31. data/lib/grape-swagger/doc_methods/file_params.rb +17 -0
  32. data/lib/grape-swagger/doc_methods/format_data.rb +53 -0
  33. data/lib/grape-swagger/doc_methods/headers.rb +20 -0
  34. data/lib/grape-swagger/doc_methods/move_params.rb +209 -0
  35. data/lib/grape-swagger/doc_methods/operation_id.rb +32 -0
  36. data/lib/grape-swagger/doc_methods/optional_object.rb +30 -0
  37. data/lib/grape-swagger/doc_methods/parse_params.rb +190 -0
  38. data/lib/grape-swagger/doc_methods/path_string.rb +52 -0
  39. data/lib/grape-swagger/doc_methods/produces_consumes.rb +15 -0
  40. data/lib/grape-swagger/doc_methods/status_codes.rb +21 -0
  41. data/lib/grape-swagger/doc_methods/tag_name_description.rb +34 -0
  42. data/lib/grape-swagger/doc_methods/version.rb +20 -0
  43. data/lib/grape-swagger/doc_methods.rb +142 -0
  44. data/lib/grape-swagger/endpoint/params_parser.rb +76 -0
  45. data/lib/grape-swagger/endpoint.rb +476 -0
  46. data/lib/grape-swagger/errors.rb +17 -0
  47. data/lib/grape-swagger/instance.rb +7 -0
  48. data/lib/grape-swagger/model_parsers.rb +42 -0
  49. data/lib/grape-swagger/rake/oapi_tasks.rb +135 -0
  50. data/lib/grape-swagger/version.rb +5 -0
  51. data/lib/grape-swagger.rb +174 -0
  52. data/spec/issues/267_nested_namespaces.rb +55 -0
  53. data/spec/issues/403_versions_spec.rb +124 -0
  54. data/spec/issues/427_entity_as_string_spec.rb +39 -0
  55. data/spec/issues/430_entity_definitions_spec.rb +94 -0
  56. data/spec/issues/532_allow_custom_format_spec.rb +42 -0
  57. data/spec/issues/533_specify_status_code_spec.rb +78 -0
  58. data/spec/issues/537_enum_values_spec.rb +50 -0
  59. data/spec/issues/539_array_post_body_spec.rb +65 -0
  60. data/spec/issues/542_array_of_type_in_post_body_spec.rb +46 -0
  61. data/spec/issues/553_align_array_put_post_params_spec.rb +152 -0
  62. data/spec/issues/572_array_post_body_spec.rb +51 -0
  63. data/spec/issues/579_align_put_post_parameters_spec.rb +185 -0
  64. data/spec/issues/582_file_response_spec.rb +55 -0
  65. data/spec/issues/587_range_parameter_delimited_by_dash_spec.rb +26 -0
  66. data/spec/issues/605_root_route_documentation_spec.rb +23 -0
  67. data/spec/issues/650_params_array_spec.rb +65 -0
  68. data/spec/issues/677_consumes_produces_add_swagger_documentation_options_spec.rb +100 -0
  69. data/spec/issues/680_keep_204_error_schemas_spec.rb +55 -0
  70. data/spec/issues/721_set_default_parameter_location_based_on_consumes_spec.rb +62 -0
  71. data/spec/issues/751_deeply_nested_objects_spec.rb +190 -0
  72. data/spec/issues/776_multiple_presents_spec.rb +59 -0
  73. data/spec/issues/784_extensions_on_params_spec.rb +42 -0
  74. data/spec/issues/809_utf8_routes_spec.rb +55 -0
  75. data/spec/issues/832_array_hash_float_decimal_spec.rb +114 -0
  76. data/spec/issues/847_route_param_options_spec.rb +37 -0
  77. data/spec/issues/873_wildcard_segments_path_parameters_spec.rb +28 -0
  78. data/spec/issues/878_optional_path_segments_spec.rb +29 -0
  79. data/spec/issues/881_handle_file_params_spec.rb +38 -0
  80. data/spec/issues/883_query_array_parameter_spec.rb +46 -0
  81. data/spec/issues/884_dont_document_non_schema_examples_spec.rb +49 -0
  82. data/spec/issues/887_prevent_duplicate_operation_ids_spec.rb +35 -0
  83. data/spec/lib/data_type_spec.rb +111 -0
  84. data/spec/lib/endpoint/params_parser_spec.rb +124 -0
  85. data/spec/lib/endpoint_spec.rb +153 -0
  86. data/spec/lib/extensions_spec.rb +185 -0
  87. data/spec/lib/format_data_spec.rb +115 -0
  88. data/spec/lib/model_parsers_spec.rb +104 -0
  89. data/spec/lib/move_params_spec.rb +444 -0
  90. data/spec/lib/oapi_tasks_spec.rb +163 -0
  91. data/spec/lib/operation_id_spec.rb +55 -0
  92. data/spec/lib/optional_object_spec.rb +47 -0
  93. data/spec/lib/parse_params_spec.rb +68 -0
  94. data/spec/lib/path_string_spec.rb +101 -0
  95. data/spec/lib/produces_consumes_spec.rb +116 -0
  96. data/spec/lib/tag_name_description_spec.rb +80 -0
  97. data/spec/lib/version_spec.rb +28 -0
  98. data/spec/spec_helper.rb +39 -0
  99. data/spec/support/empty_model_parser.rb +23 -0
  100. data/spec/support/grape_version.rb +13 -0
  101. data/spec/support/mock_parser.rb +23 -0
  102. data/spec/support/model_parsers/entity_parser.rb +334 -0
  103. data/spec/support/model_parsers/mock_parser.rb +346 -0
  104. data/spec/support/model_parsers/representable_parser.rb +406 -0
  105. data/spec/support/namespace_tags.rb +93 -0
  106. data/spec/support/the_paths_definitions.rb +109 -0
  107. data/spec/swagger_v2/api_documentation_spec.rb +42 -0
  108. data/spec/swagger_v2/api_swagger_v2_additional_properties_spec.rb +83 -0
  109. data/spec/swagger_v2/api_swagger_v2_body_definitions_spec.rb +48 -0
  110. data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +36 -0
  111. data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +79 -0
  112. data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +145 -0
  113. data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +137 -0
  114. data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +56 -0
  115. data/spec/swagger_v2/api_swagger_v2_hash_and_array_spec.rb +64 -0
  116. data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +58 -0
  117. data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +57 -0
  118. data/spec/swagger_v2/api_swagger_v2_hide_param_spec.rb +109 -0
  119. data/spec/swagger_v2/api_swagger_v2_ignore_defaults_spec.rb +48 -0
  120. data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +153 -0
  121. data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +355 -0
  122. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +217 -0
  123. data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +247 -0
  124. data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +80 -0
  125. data/spec/swagger_v2/api_swagger_v2_response_spec.rb +147 -0
  126. data/spec/swagger_v2/api_swagger_v2_response_with_examples_spec.rb +135 -0
  127. data/spec/swagger_v2/api_swagger_v2_response_with_headers_spec.rb +216 -0
  128. data/spec/swagger_v2/api_swagger_v2_response_with_models_spec.rb +53 -0
  129. data/spec/swagger_v2/api_swagger_v2_response_with_root_spec.rb +153 -0
  130. data/spec/swagger_v2/api_swagger_v2_spec.rb +245 -0
  131. data/spec/swagger_v2/api_swagger_v2_status_codes_spec.rb +93 -0
  132. data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +90 -0
  133. data/spec/swagger_v2/boolean_params_spec.rb +38 -0
  134. data/spec/swagger_v2/default_api_spec.rb +175 -0
  135. data/spec/swagger_v2/deprecated_field_spec.rb +25 -0
  136. data/spec/swagger_v2/description_not_initialized_spec.rb +39 -0
  137. data/spec/swagger_v2/endpoint_versioned_path_spec.rb +130 -0
  138. data/spec/swagger_v2/errors_spec.rb +77 -0
  139. data/spec/swagger_v2/float_api_spec.rb +36 -0
  140. data/spec/swagger_v2/form_params_spec.rb +76 -0
  141. data/spec/swagger_v2/grape-swagger_spec.rb +17 -0
  142. data/spec/swagger_v2/guarded_endpoint_spec.rb +162 -0
  143. data/spec/swagger_v2/hide_api_spec.rb +147 -0
  144. data/spec/swagger_v2/host_spec.rb +43 -0
  145. data/spec/swagger_v2/inheritance_and_discriminator_spec.rb +57 -0
  146. data/spec/swagger_v2/mount_override_api_spec.rb +58 -0
  147. data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
  148. data/spec/swagger_v2/namespace_tags_prefix_spec.rb +122 -0
  149. data/spec/swagger_v2/namespace_tags_spec.rb +78 -0
  150. data/spec/swagger_v2/namespaced_api_spec.rb +121 -0
  151. data/spec/swagger_v2/nicknamed_api_spec.rb +25 -0
  152. data/spec/swagger_v2/operation_id_api_spec.rb +27 -0
  153. data/spec/swagger_v2/param_multi_type_spec.rb +82 -0
  154. data/spec/swagger_v2/param_type_spec.rb +95 -0
  155. data/spec/swagger_v2/param_values_spec.rb +180 -0
  156. data/spec/swagger_v2/params_array_collection_format_spec.rb +105 -0
  157. data/spec/swagger_v2/params_array_spec.rb +225 -0
  158. data/spec/swagger_v2/params_example_spec.rb +38 -0
  159. data/spec/swagger_v2/params_hash_spec.rb +77 -0
  160. data/spec/swagger_v2/params_nested_spec.rb +92 -0
  161. data/spec/swagger_v2/parent_less_namespace_spec.rb +32 -0
  162. data/spec/swagger_v2/reference_entity_spec.rb +129 -0
  163. data/spec/swagger_v2/security_requirement_spec.rb +46 -0
  164. data/spec/swagger_v2/simple_mounted_api_spec.rb +332 -0
  165. data/spec/version_spec.rb +10 -0
  166. metadata +225 -0
@@ -0,0 +1,247 @@
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
+ consumes: ['application/x-www-form-urlencoded'],
14
+ success: Entities::UseResponse
15
+ params do
16
+ optional :in_query, type: String, documentation: { param_type: 'query' }
17
+ optional :in_header, type: String, documentation: { param_type: 'header' }
18
+ end
19
+
20
+ get '/defined_param_type' do
21
+ { 'declared_params' => declared(params) }
22
+ end
23
+
24
+ desc 'full set of request param types',
25
+ consumes: ['application/x-www-form-urlencoded'],
26
+ success: Entities::UseResponse
27
+ params do
28
+ requires :in_path, type: Integer
29
+ optional :in_query, type: String, documentation: { param_type: 'query' }
30
+ optional :in_header, type: String, documentation: { param_type: 'header' }
31
+ end
32
+
33
+ get '/defined_param_type/:in_path' do
34
+ { 'declared_params' => declared(params) }
35
+ end
36
+
37
+ desc 'full set of request param types',
38
+ consumes: ['application/x-www-form-urlencoded'],
39
+ success: Entities::UseResponse
40
+ params do
41
+ optional :in_path, type: Integer
42
+ optional :in_query, type: String, documentation: { param_type: 'query' }
43
+ optional :in_header, type: String, documentation: { param_type: 'header' }
44
+ end
45
+
46
+ delete '/defined_param_type/:in_path' do
47
+ { 'declared_params' => declared(params) }
48
+ end
49
+
50
+ # using `:in`
51
+ desc 'full set of request param types using `:in`',
52
+ consumes: ['application/x-www-form-urlencoded'],
53
+ success: Entities::UseResponse
54
+ params do
55
+ optional :in_query, type: String, documentation: { in: 'query' }
56
+ optional :in_header, type: String, documentation: { in: 'header' }
57
+ end
58
+
59
+ get '/defined_in' do
60
+ { 'declared_params' => declared(params) }
61
+ end
62
+
63
+ desc 'full set of request param types using `:in`',
64
+ consumes: ['application/x-www-form-urlencoded'],
65
+ success: Entities::UseResponse
66
+ params do
67
+ requires :in_path, type: Integer
68
+ optional :in_query, type: String, documentation: { in: 'query' }
69
+ optional :in_header, type: String, documentation: { in: 'header' }
70
+ end
71
+
72
+ get '/defined_in/:in_path' do
73
+ { 'declared_params' => declared(params) }
74
+ end
75
+
76
+ desc 'full set of request param types using `:in`',
77
+ consumes: ['application/x-www-form-urlencoded']
78
+ params do
79
+ optional :in_path, type: Integer
80
+ optional :in_query, type: String, documentation: { in: 'query' }
81
+ optional :in_header, type: String, documentation: { in: 'header' }
82
+ end
83
+
84
+ delete '/defined_in/:in_path' do
85
+ { 'declared_params' => declared(params) }
86
+ end
87
+
88
+ # file
89
+ desc 'file download',
90
+ consumes: ['application/x-www-form-urlencoded'],
91
+ success: Entities::UseResponse
92
+ params do
93
+ requires :name, type: String
94
+ end
95
+
96
+ get '/download' do
97
+ { 'declared_params' => declared(params) }
98
+ end
99
+
100
+ desc 'file upload',
101
+ consumes: ['application/x-www-form-urlencoded'],
102
+ success: Entities::UseResponse
103
+ params do
104
+ requires :name, type: File
105
+ end
106
+
107
+ post '/upload' do
108
+ { 'declared_params' => declared(params) }
109
+ end
110
+
111
+ add_swagger_documentation
112
+ end
113
+ end
114
+ end
115
+
116
+ def app
117
+ TheApi::ParamTypeApi
118
+ end
119
+
120
+ describe 'foo' do
121
+ subject do
122
+ get '/swagger_doc'
123
+ JSON.parse(last_response.body)
124
+ end
125
+
126
+ specify do
127
+ expect(subject['paths']['/defined_param_type/{in_path}']['delete']['responses']).to eql(
128
+ '200' => {
129
+ 'description' => 'full set of request param types',
130
+ 'schema' => { '$ref' => '#/definitions/UseResponse' }
131
+ }
132
+ )
133
+ end
134
+
135
+ specify do
136
+ expect(subject['paths']['/defined_in/{in_path}']['delete']['responses']).to eql(
137
+ '204' => {
138
+ 'description' => 'full set of request param types using `:in`'
139
+ }
140
+ )
141
+ end
142
+ end
143
+
144
+ describe 'defined param types' do
145
+ subject do
146
+ get '/swagger_doc/defined_param_type'
147
+ JSON.parse(last_response.body)
148
+ end
149
+
150
+ specify do
151
+ expect(subject['paths']['/defined_param_type']['get']['parameters']).to eql(
152
+ [
153
+ { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'type' => 'string' },
154
+ { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'type' => 'string' }
155
+ ]
156
+ )
157
+ end
158
+
159
+ specify do
160
+ expect(subject['paths']['/defined_param_type/{in_path}']['get']['parameters']).to eql(
161
+ [
162
+ { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'type' => 'integer', 'format' => 'int32' },
163
+ { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'type' => 'string' },
164
+ { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'type' => 'string' }
165
+ ]
166
+ )
167
+ end
168
+
169
+ specify do
170
+ expect(subject['paths']['/defined_param_type/{in_path}']['delete']['parameters']).to eql(
171
+ [
172
+ { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'type' => 'integer', 'format' => 'int32' },
173
+ { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'type' => 'string' },
174
+ { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'type' => 'string' }
175
+ ]
176
+ )
177
+ end
178
+ end
179
+
180
+ describe 'defined param types with `:in`' do
181
+ subject do
182
+ get '/swagger_doc/defined_in'
183
+ JSON.parse(last_response.body)
184
+ end
185
+
186
+ specify do
187
+ expect(subject['paths']['/defined_in']['get']['parameters']).to eql(
188
+ [
189
+ { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'type' => 'string' },
190
+ { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'type' => 'string' }
191
+ ]
192
+ )
193
+ end
194
+
195
+ specify do
196
+ expect(subject['paths']['/defined_in/{in_path}']['get']['parameters']).to eql(
197
+ [
198
+ { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'type' => 'integer', 'format' => 'int32' },
199
+ { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'type' => 'string' },
200
+ { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'type' => 'string' }
201
+ ]
202
+ )
203
+ end
204
+
205
+ specify do
206
+ expect(subject['paths']['/defined_in/{in_path}']['delete']['parameters']).to eql(
207
+ [
208
+ { 'in' => 'path', 'name' => 'in_path', 'required' => true, 'type' => 'integer', 'format' => 'int32' },
209
+ { 'in' => 'query', 'name' => 'in_query', 'required' => false, 'type' => 'string' },
210
+ { 'in' => 'header', 'name' => 'in_header', 'required' => false, 'type' => 'string' }
211
+ ]
212
+ )
213
+ end
214
+ end
215
+
216
+ describe 'file' do
217
+ describe 'upload' do
218
+ subject do
219
+ get '/swagger_doc/upload'
220
+ JSON.parse(last_response.body)
221
+ end
222
+
223
+ specify do
224
+ expect(subject['paths']['/upload']['post']['parameters']).to eql(
225
+ [
226
+ { 'in' => 'formData', 'name' => 'name', 'required' => true, 'type' => 'file' }
227
+ ]
228
+ )
229
+ end
230
+ end
231
+
232
+ describe 'download' do
233
+ subject do
234
+ get '/swagger_doc/download'
235
+ JSON.parse(last_response.body)
236
+ end
237
+
238
+ specify do
239
+ expect(subject['paths']['/download']['get']['parameters']).to eql(
240
+ [
241
+ { 'in' => 'query', 'name' => 'name', 'required' => true, 'type' => 'string' }
242
+ ]
243
+ )
244
+ end
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'additional parameter settings' do
6
+ before :all do
7
+ module TheApi
8
+ class RequestParamFix < Grape::API
9
+ resource :bookings do
10
+ desc 'Update booking' do
11
+ consumes ['application/x-www-form-urlencoded']
12
+ end
13
+ params do
14
+ optional :name, type: String
15
+ end
16
+ put ':id' do
17
+ { 'declared_params' => declared(params) }
18
+ end
19
+
20
+ desc 'Get booking details' do
21
+ consumes ['application/x-www-form-urlencoded']
22
+ end
23
+ get ':id' do
24
+ { 'declared_params' => declared(params) }
25
+ end
26
+
27
+ desc 'Get booking details by access_number' do
28
+ consumes ['application/x-www-form-urlencoded']
29
+ end
30
+ get '/conf/:access_number' do
31
+ { 'declared_params' => declared(params) }
32
+ end
33
+
34
+ desc 'Remove booking' do
35
+ consumes ['application/x-www-form-urlencoded']
36
+ end
37
+ delete ':id' do
38
+ { 'declared_params' => declared(params) }
39
+ end
40
+ end
41
+
42
+ add_swagger_documentation
43
+ end
44
+ end
45
+ end
46
+
47
+ def app
48
+ TheApi::RequestParamFix
49
+ end
50
+
51
+ subject do
52
+ get '/swagger_doc'
53
+ JSON.parse(last_response.body)
54
+ end
55
+
56
+ specify do
57
+ expect(subject['paths']['/bookings/{id}']['put']['parameters'].sort_by { |p| p['name'] }).to eql(
58
+ [
59
+ { 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true },
60
+ { 'in' => 'formData', 'name' => 'name', 'type' => 'string', 'required' => false }
61
+ ]
62
+ )
63
+ end
64
+
65
+ specify do
66
+ expect(subject['paths']['/bookings/{id}']['get']['parameters']).to eql(
67
+ [
68
+ { 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }
69
+ ]
70
+ )
71
+ end
72
+
73
+ specify do
74
+ expect(subject['paths']['/bookings/{id}']['delete']['parameters']).to eql(
75
+ [
76
+ { 'in' => 'path', 'name' => 'id', 'type' => 'integer', 'format' => 'int32', 'required' => true }
77
+ ]
78
+ )
79
+ end
80
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'response' do
6
+ include_context "#{MODEL_PARSER} swagger example"
7
+
8
+ before :all do
9
+ module TheApi
10
+ class ResponseApi < Grape::API
11
+ format :json
12
+
13
+ desc 'This returns something',
14
+ consumes: ['application/x-www-form-urlencoded'],
15
+ params: Entities::UseResponse.documentation,
16
+ failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
17
+ post '/params_given' do
18
+ { 'declared_params' => declared(params) }
19
+ end
20
+
21
+ desc 'This returns something',
22
+ consumes: ['application/x-www-form-urlencoded'],
23
+ entity: Entities::UseResponse,
24
+ failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
25
+ get '/entity_response' do
26
+ { 'declared_params' => declared(params) }
27
+ end
28
+
29
+ desc 'This returns something',
30
+ consumes: ['application/x-www-form-urlencoded'],
31
+ entity: Entities::UseItemResponseAsType,
32
+ failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
33
+ get '/nested_type' do
34
+ { 'declared_params' => declared(params) }
35
+ end
36
+
37
+ desc 'This returns something',
38
+ consumes: ['application/x-www-form-urlencoded'],
39
+ success: [
40
+ { code: 200, message: 'Request has succeeded' },
41
+ { code: 201, message: 'Successful Operation' },
42
+ { code: 204, message: 'Request was fulfilled' }
43
+ ],
44
+ failure: [{ code: 400, message: 'NotFound', model: Entities::ApiError }]
45
+ get '/multiple-success-responses' do
46
+ { 'declared_params' => declared(params) }
47
+ end
48
+
49
+ add_swagger_documentation
50
+ end
51
+ end
52
+ end
53
+
54
+ def app
55
+ TheApi::ResponseApi
56
+ end
57
+
58
+ describe 'uses nested type as response object' do
59
+ subject do
60
+ get '/swagger_doc/nested_type'
61
+ JSON.parse(last_response.body)
62
+ end
63
+ specify do
64
+ expect(subject['paths']['/nested_type']['get']).to eql(
65
+ 'description' => 'This returns something',
66
+ 'produces' => ['application/json'],
67
+ 'responses' => {
68
+ '200' => { 'description' => 'This returns something', 'schema' => { '$ref' => '#/definitions/UseItemResponseAsType' } },
69
+ '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } }
70
+ },
71
+ 'tags' => ['nested_type'],
72
+ 'operationId' => 'getNestedType'
73
+ )
74
+ expect(subject['definitions']).to eql(swagger_nested_type)
75
+ end
76
+ end
77
+
78
+ describe 'uses entity as response object' do
79
+ subject do
80
+ get '/swagger_doc/entity_response'
81
+ JSON.parse(last_response.body)
82
+ end
83
+
84
+ specify do
85
+ expect(subject['paths']['/entity_response']['get']).to eql(
86
+ 'description' => 'This returns something',
87
+ 'produces' => ['application/json'],
88
+ 'responses' => {
89
+ '200' => { 'description' => 'This returns something', 'schema' => { '$ref' => '#/definitions/UseResponse' } },
90
+ '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } }
91
+ },
92
+ 'tags' => ['entity_response'],
93
+ 'operationId' => 'getEntityResponse'
94
+ )
95
+ expect(subject['definitions']).to eql(swagger_entity_as_response_object)
96
+ end
97
+ end
98
+
99
+ describe 'uses params as response object' do
100
+ subject do
101
+ get '/swagger_doc/params_given'
102
+ JSON.parse(last_response.body)
103
+ end
104
+
105
+ specify do
106
+ expect(subject['paths']['/params_given']['post']).to eql(
107
+ 'description' => 'This returns something',
108
+ 'produces' => ['application/json'],
109
+ 'consumes' => ['application/x-www-form-urlencoded'],
110
+ 'parameters' => [
111
+ { 'in' => 'formData', 'name' => 'description', 'type' => 'string', 'required' => false },
112
+ { 'in' => 'formData', 'name' => '$responses', 'type' => 'array', 'items' => { 'type' => 'string' }, 'required' => false }
113
+ ],
114
+ 'responses' => {
115
+ '201' => { 'description' => 'This returns something' },
116
+ '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } }
117
+ },
118
+ 'tags' => ['params_given'],
119
+ 'operationId' => 'postParamsGiven'
120
+ )
121
+ expect(subject['definitions']).to eql(swagger_params_as_response_object)
122
+ end
123
+ end
124
+
125
+ describe 'uses params as response object when response contains multiple values for success' do
126
+ subject do
127
+ get '/swagger_doc/multiple-success-responses'
128
+ JSON.parse(last_response.body)
129
+ end
130
+
131
+ specify do
132
+ expect(subject['paths']['/multiple-success-responses']['get']).to eql(
133
+ 'description' => 'This returns something',
134
+ 'produces' => ['application/json'],
135
+ 'responses' => {
136
+ '200' => { 'description' => 'Request has succeeded' },
137
+ '201' => { 'description' => 'Successful Operation' },
138
+ '204' => { 'description' => 'Request was fulfilled' },
139
+ '400' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } }
140
+ },
141
+ 'tags' => ['multiple-success-responses'],
142
+ 'operationId' => 'getMultipleSuccessResponses'
143
+ )
144
+ expect(subject['definitions']).to eql(swagger_params_as_response_object)
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,135 @@
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 syntax also returns examples' do
22
+ success model: Entities::UseResponse, examples: { 'application/json' => { description: 'Names list', items: [{ id: '123', name: 'John' }] } }
23
+ failure [
24
+ {
25
+ code: 404,
26
+ message: 'NotFound',
27
+ model: Entities::ApiError,
28
+ examples: { 'application/json' => { code: 404, message: 'Not found' } }
29
+ },
30
+ {
31
+ code: 400,
32
+ message: 'BadRequest',
33
+ model: Entities::ApiError,
34
+ examples: { 'application/json' => { code: 400, message: 'Bad Request' } }
35
+ }
36
+ ]
37
+ end
38
+ get '/response_failure_examples' do
39
+ { 'declared_params' => declared(params) }
40
+ end
41
+
42
+ desc 'This does not return examples' do
43
+ success model: Entities::UseResponse
44
+ failure [[404, 'NotFound', Entities::ApiError]]
45
+ end
46
+ get '/response_no_examples' do
47
+ { 'declared_params' => declared(params) }
48
+ end
49
+ add_swagger_documentation
50
+ end
51
+ end
52
+ end
53
+
54
+ def app
55
+ TheApi::ResponseApiExamples
56
+ end
57
+
58
+ describe 'response examples' do
59
+ let(:example_200) do
60
+ { 'application/json' => { 'description' => 'Names list', 'items' => [{ 'id' => '123', 'name' => 'John' }] } }
61
+ end
62
+ let(:example_404) do
63
+ { 'application/json' => { 'code' => 404, 'message' => 'Not found' } }
64
+ end
65
+
66
+ subject do
67
+ get '/swagger_doc/response_examples'
68
+ JSON.parse(last_response.body)
69
+ end
70
+
71
+ specify do
72
+ expect(subject['paths']['/response_examples']['get']).to eql(
73
+ 'description' => 'This returns examples',
74
+ 'produces' => ['application/json'],
75
+ 'responses' => {
76
+ '200' => { 'description' => 'This returns examples', 'schema' => { '$ref' => '#/definitions/UseResponse' }, 'examples' => example_200 },
77
+ '404' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' }, 'examples' => example_404 }
78
+ },
79
+ 'tags' => ['response_examples'],
80
+ 'operationId' => 'getResponseExamples'
81
+ )
82
+ end
83
+ end
84
+
85
+ describe 'response failure examples' do
86
+ let(:example_200) do
87
+ { 'application/json' => { 'description' => 'Names list', 'items' => [{ 'id' => '123', 'name' => 'John' }] } }
88
+ end
89
+ let(:example_404) do
90
+ { 'application/json' => { 'code' => 404, 'message' => 'Not found' } }
91
+ end
92
+ let(:example_400) do
93
+ { 'application/json' => { 'code' => 400, 'message' => 'Bad Request' } }
94
+ end
95
+
96
+ subject do
97
+ get '/swagger_doc/response_failure_examples'
98
+ JSON.parse(last_response.body)
99
+ end
100
+
101
+ specify do
102
+ expect(subject['paths']['/response_failure_examples']['get']).to eql(
103
+ 'description' => 'This syntax also returns examples',
104
+ 'produces' => ['application/json'],
105
+ 'responses' => {
106
+ '200' => { 'description' => 'This syntax also returns examples', 'schema' => { '$ref' => '#/definitions/UseResponse' }, 'examples' => example_200 },
107
+ '404' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' }, 'examples' => example_404 },
108
+ '400' => { 'description' => 'BadRequest', 'schema' => { '$ref' => '#/definitions/ApiError' }, 'examples' => example_400 }
109
+ },
110
+ 'tags' => ['response_failure_examples'],
111
+ 'operationId' => 'getResponseFailureExamples'
112
+ )
113
+ end
114
+ end
115
+
116
+ describe 'response no examples' do
117
+ subject do
118
+ get '/swagger_doc/response_no_examples'
119
+ JSON.parse(last_response.body)
120
+ end
121
+
122
+ specify do
123
+ expect(subject['paths']['/response_no_examples']['get']).to eql(
124
+ 'description' => 'This does not return examples',
125
+ 'produces' => ['application/json'],
126
+ 'responses' => {
127
+ '200' => { 'description' => 'This does not return examples', 'schema' => { '$ref' => '#/definitions/UseResponse' } },
128
+ '404' => { 'description' => 'NotFound', 'schema' => { '$ref' => '#/definitions/ApiError' } }
129
+ },
130
+ 'tags' => ['response_no_examples'],
131
+ 'operationId' => 'getResponseNoExamples'
132
+ )
133
+ end
134
+ end
135
+ end