grape-swagger 0.11.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +8 -1
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +14 -22
  5. data/.travis.yml +7 -4
  6. data/CHANGELOG.md +53 -26
  7. data/Gemfile +1 -1
  8. data/README.md +414 -327
  9. data/RELEASING.md +3 -4
  10. data/example/api/endpoints.rb +132 -0
  11. data/example/api/entities.rb +18 -0
  12. data/example/config.ru +36 -2
  13. data/example/example_requests.postman_collection +146 -0
  14. data/example/swagger-example.png +0 -0
  15. data/grape-swagger.gemspec +9 -6
  16. data/lib/grape-swagger.rb +69 -99
  17. data/lib/grape-swagger/doc_methods.rb +69 -544
  18. data/lib/grape-swagger/doc_methods/data_type.rb +77 -0
  19. data/lib/grape-swagger/doc_methods/extensions.rb +75 -0
  20. data/lib/grape-swagger/doc_methods/move_params.rb +153 -0
  21. data/lib/grape-swagger/doc_methods/operation_id.rb +27 -0
  22. data/lib/grape-swagger/doc_methods/optional_object.rb +15 -0
  23. data/lib/grape-swagger/doc_methods/parse_params.rb +113 -0
  24. data/lib/grape-swagger/doc_methods/path_string.rb +29 -0
  25. data/lib/grape-swagger/doc_methods/produces_consumes.rb +12 -0
  26. data/lib/grape-swagger/doc_methods/status_codes.rb +17 -0
  27. data/lib/grape-swagger/doc_methods/tag_name_description.rb +26 -0
  28. data/lib/grape-swagger/endpoint.rb +317 -0
  29. data/lib/grape-swagger/version.rb +1 -1
  30. data/spec/lib/data_type_spec.rb +57 -0
  31. data/spec/lib/endpoint_spec.rb +6 -0
  32. data/spec/lib/extensions_spec.rb +127 -0
  33. data/spec/lib/move_params_spec.rb +298 -0
  34. data/spec/lib/operation_id_spec.rb +24 -0
  35. data/spec/lib/optional_object_spec.rb +40 -0
  36. data/spec/lib/path_string_spec.rb +38 -0
  37. data/spec/lib/produces_consumes_spec.rb +98 -0
  38. data/spec/markdown/kramdown_adapter_spec.rb +2 -9
  39. data/spec/markdown/redcarpet_adapter_spec.rb +2 -16
  40. data/spec/spec_helper.rb +7 -13
  41. data/spec/support/api_swagger_v2_result.rb +204 -0
  42. data/spec/support/namespace_tags.rb +73 -0
  43. data/spec/support/the_api_entities.rb +52 -0
  44. data/spec/support/the_paths_definitions.rb +94 -0
  45. data/spec/swagger_v2/api_swagger_v2_definitions-models_spec.rb +32 -0
  46. data/spec/swagger_v2/api_swagger_v2_detail_spec.rb +151 -0
  47. data/spec/swagger_v2/api_swagger_v2_extensions_spec.rb +109 -0
  48. data/spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb +124 -0
  49. data/spec/swagger_v2/api_swagger_v2_global_configuration_spec.rb +51 -0
  50. data/spec/swagger_v2/api_swagger_v2_headers_spec.rb +44 -0
  51. data/spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb +56 -0
  52. data/spec/swagger_v2/api_swagger_v2_mounted_spec.rb +146 -0
  53. data/spec/swagger_v2/api_swagger_v2_param_type_body_nested_spec.rb +197 -0
  54. data/spec/swagger_v2/api_swagger_v2_param_type_body_spec.rb +151 -0
  55. data/spec/swagger_v2/api_swagger_v2_param_type_spec.rb +217 -0
  56. data/spec/swagger_v2/api_swagger_v2_request_params_fix_spec.rb +64 -0
  57. data/spec/swagger_v2/api_swagger_v2_response_spec.rb +184 -0
  58. data/spec/swagger_v2/api_swagger_v2_spec.rb +207 -0
  59. data/spec/swagger_v2/api_swagger_v2_type-format_spec.rb +121 -0
  60. data/spec/{boolean_params_spec.rb → swagger_v2/boolean_params_spec.rb} +2 -2
  61. data/spec/{default_api_spec.rb → swagger_v2/default_api_spec.rb} +40 -36
  62. data/spec/swagger_v2/description_not_initialized.rb +39 -0
  63. data/spec/{float_api_spec.rb → swagger_v2/float_api_spec.rb} +2 -2
  64. data/spec/{form_params_spec.rb → swagger_v2/form_params_spec.rb} +9 -9
  65. data/spec/{grape-swagger_spec.rb → swagger_v2/grape-swagger_spec.rb} +0 -0
  66. data/spec/swagger_v2/hide_api_spec.rb +131 -0
  67. data/spec/swagger_v2/mounted_target_class_spec.rb +76 -0
  68. data/spec/swagger_v2/namespace_tags_prefix_spec.rb +84 -0
  69. data/spec/swagger_v2/namespace_tags_spec.rb +76 -0
  70. data/spec/{namespaced_api_spec.rb → swagger_v2/namespaced_api_spec.rb} +6 -26
  71. data/spec/{param_type_spec.rb → swagger_v2/param_type_spec.rb} +10 -8
  72. data/spec/{param_values_spec.rb → swagger_v2/param_values_spec.rb} +52 -22
  73. data/spec/swagger_v2/params_array_spec.rb +63 -0
  74. data/spec/swagger_v2/params_hash_spec.rb +65 -0
  75. data/spec/swagger_v2/params_nested_spec.rb +63 -0
  76. data/spec/{reference_entity.rb → swagger_v2/reference_entity.rb} +18 -23
  77. data/spec/swagger_v2/response_model_spec.rb +212 -0
  78. data/spec/swagger_v2/simple_mounted_api_spec.rb +264 -0
  79. metadata +175 -90
  80. data/example/api.rb +0 -66
  81. data/lib/grape-swagger/markdown.rb +0 -23
  82. data/spec/api_description_spec.rb +0 -43
  83. data/spec/api_global_models_spec.rb +0 -77
  84. data/spec/api_models_spec.rb +0 -364
  85. data/spec/api_paths_spec.rb +0 -128
  86. data/spec/api_root_spec.rb +0 -30
  87. data/spec/api_with_nil_types.rb +0 -50
  88. data/spec/api_with_path_versioning_spec.rb +0 -33
  89. data/spec/api_with_prefix_and_namespace_spec.rb +0 -32
  90. data/spec/api_with_standalone_namespace_spec.rb +0 -215
  91. data/spec/array_entity_spec.rb +0 -34
  92. data/spec/array_params_spec.rb +0 -85
  93. data/spec/grape-swagger_helper_spec.rb +0 -152
  94. data/spec/group_params_spec.rb +0 -31
  95. data/spec/hash_params_spec.rb +0 -30
  96. data/spec/hide_api_spec.rb +0 -124
  97. data/spec/i18n_spec.rb +0 -364
  98. data/spec/markdown/markdown_spec.rb +0 -27
  99. data/spec/mounted_target_class_spec.rb +0 -63
  100. data/spec/mutually_exclusive_spec.rb +0 -36
  101. data/spec/non_default_api_spec.rb +0 -733
  102. data/spec/response_model_spec.rb +0 -121
  103. data/spec/simple_mounted_api_spec.rb +0 -213
  104. data/spec/support/i18n_helper.rb +0 -8
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `header`' do
4
+ include_context "the api entities"
5
+
6
+ before :all do
7
+ module TheApi
8
+ class BodyParamTypeApi < Grape::API
9
+ namespace :wo_entities do
10
+ desc 'post in body /wo entity'
11
+ params do
12
+ requires :in_body_1, type: Integer, documentation: { desc: 'in_body_1', param_type: 'body' }
13
+ optional :in_body_2, type: String, documentation: { desc: 'in_body_2', param_type: 'body' }
14
+ optional :in_body_3, type: String, documentation: { desc: 'in_body_3', param_type: 'body' }
15
+ end
16
+
17
+ post '/in_body' do
18
+ { "declared_params" => declared(params) }
19
+ end
20
+
21
+ desc 'put in body /wo entity'
22
+ params do
23
+ requires :key, type: Integer
24
+ optional :in_body_1, type: Integer, documentation: { desc: 'in_body_1', param_type: 'body' }
25
+ optional :in_body_2, type: String, documentation: { desc: 'in_body_2', param_type: 'body' }
26
+ optional :in_body_3, type: String, documentation: { desc: 'in_body_3', param_type: 'body' }
27
+ end
28
+
29
+ put '/in_body/:key' do
30
+ { "declared_params" => declared(params) }
31
+ end
32
+ end
33
+
34
+ namespace :with_entities do
35
+ desc 'post in body with entity',
36
+ success: TheApi::Entities::ResponseItem
37
+ params do
38
+ requires :name, type: String, documentation: { desc: 'name', param_type: 'body' }
39
+ end
40
+
41
+ post '/in_body' do
42
+ { "declared_params" => declared(params) }
43
+ end
44
+
45
+ desc 'put in body with entity',
46
+ success: TheApi::Entities::ResponseItem
47
+ params do
48
+ requires :id, type: Integer
49
+ optional :name, type: String, documentation: { desc: 'name', param_type: 'body' }
50
+ end
51
+
52
+ put '/in_body/:id' do
53
+ { "declared_params" => declared(params) }
54
+ end
55
+ end
56
+
57
+ add_swagger_documentation
58
+ end
59
+ end
60
+ end
61
+
62
+ def app
63
+ TheApi::BodyParamTypeApi
64
+ end
65
+
66
+ describe 'no entity given' do
67
+ subject do
68
+ get '/swagger_doc/wo_entities'
69
+ JSON.parse(last_response.body)
70
+ end
71
+
72
+ specify do
73
+ expect(subject['paths']['/wo_entities/in_body']['post']['parameters']).to eql([
74
+ {"name"=>"InBody", "in"=>"body", "required"=>true, "schema"=>{"$ref"=>"#/definitions/postRequestInBody"}}
75
+ ])
76
+ end
77
+
78
+ specify do
79
+ expect(subject['definitions']['postRequestInBody']).to eql({
80
+ "description" => "post in body /wo entity",
81
+ "type"=>"object",
82
+ "properties"=>{
83
+ "in_body_1"=>{"type"=>"integer", "format"=>"int32", "description"=>"in_body_1"},
84
+ "in_body_2"=>{"type"=>"string", "description"=>"in_body_2"},
85
+ "in_body_3"=>{"type"=>"string", "description"=>"in_body_3"}
86
+ },
87
+ "required"=>["in_body_1"]
88
+ })
89
+ end
90
+
91
+ specify do
92
+ expect(subject['paths']['/wo_entities/in_body/{key}']['put']['parameters']).to eql([
93
+ {"in"=>"path", "name"=>"key", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true},
94
+ {"name"=>"InBody", "in"=>"body", "required"=>true, "schema"=>{"$ref"=>"#/definitions/putRequestInBody"}}
95
+ ])
96
+ end
97
+
98
+ specify do
99
+ expect(subject['definitions']['putRequestInBody']).to eql({
100
+ "description" => "put in body /wo entity",
101
+ "type"=>"object",
102
+ "properties"=>{
103
+ "key"=>{"type"=>"integer", "format"=>"int32", "readOnly"=>true},
104
+ "in_body_1"=>{"type"=>"integer", "format"=>"int32", "description"=>"in_body_1"},
105
+ "in_body_2"=>{"type"=>"string", "description"=>"in_body_2"},
106
+ "in_body_3"=>{"type"=>"string", "description"=>"in_body_3"}
107
+ }
108
+ })
109
+ end
110
+ end
111
+
112
+ describe 'entity given' do
113
+ subject do
114
+ get '/swagger_doc/with_entities'
115
+ JSON.parse(last_response.body)
116
+ end
117
+
118
+ specify do
119
+ expect(subject['paths']['/with_entities/in_body']['post']['parameters']).to eql([
120
+ {"name"=>"ResponseItem", "in"=>"body", "required"=>true, "schema"=>{"$ref"=>"#/definitions/postRequestResponseItem"}}
121
+ ])
122
+ end
123
+
124
+ specify do
125
+ expect(subject['definitions']['postRequestResponseItem']).to eql({
126
+ "description" => "post in body with entity",
127
+ "type"=>"object",
128
+ "properties"=>{
129
+ "name"=>{"type"=>"string", "description"=>"name"}},
130
+ "required"=>["name"]
131
+ })
132
+ end
133
+
134
+ specify do
135
+ expect(subject['paths']['/with_entities/in_body/{id}']['put']['parameters']).to eql([
136
+ {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true},
137
+ {"name"=>"ResponseItem", "in"=>"body", "required"=>true, "schema"=>{"$ref"=>"#/definitions/putRequestResponseItem"}}
138
+ ])
139
+ end
140
+
141
+ specify do
142
+ expect(subject['definitions']['putRequestResponseItem']).to eql({
143
+ "description" => "put in body with entity",
144
+ "type"=>"object",
145
+ "properties"=>{
146
+ "id"=>{"type"=>"integer", "format"=>"int32", "readOnly"=>true},
147
+ "name"=>{"type"=>"string", "description"=>"name"}}
148
+ })
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,217 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'setting of param type, such as `query`, `path`, `formData`, `body`, `header`' do
4
+ include_context "the api entities"
5
+
6
+ before :all do
7
+ module TheApi
8
+ class ParamTypeApi < Grape::API
9
+ # using `:param_type`
10
+ desc 'full set of request param types',
11
+ success: TheApi::Entities::UseResponse
12
+ params do
13
+ optional :in_query, type: String, documentation: { param_type: 'query' }
14
+ optional :in_header, type: String, documentation: { param_type: 'header' }
15
+ end
16
+
17
+ get '/defined_param_type' do
18
+ { "declared_params" => declared(params) }
19
+ end
20
+
21
+ desc 'full set of request param types',
22
+ success: TheApi::Entities::UseResponse
23
+ params do
24
+ requires :in_path, type: Integer
25
+ optional :in_query, type: String, documentation: { param_type: 'query' }
26
+ optional :in_header, type: String, documentation: { param_type: 'header' }
27
+ end
28
+
29
+ get '/defined_param_type/:in_path' do
30
+ { "declared_params" => declared(params) }
31
+ end
32
+
33
+ desc 'full set of request param types',
34
+ success: TheApi::Entities::UseResponse
35
+ params do
36
+ optional :in_path, type: Integer
37
+ optional :in_query, type: String, documentation: { param_type: 'query' }
38
+ optional :in_header, type: String, documentation: { param_type: 'header' }
39
+ end
40
+
41
+ delete '/defined_param_type/:in_path' do
42
+ { "declared_params" => declared(params) }
43
+ end
44
+
45
+ # using `:in`
46
+ desc 'full set of request param types using `:in`',
47
+ success: TheApi::Entities::UseResponse
48
+ params do
49
+ optional :in_query, type: String, documentation: { in: 'query' }
50
+ optional :in_header, type: String, documentation: { in: 'header' }
51
+ end
52
+
53
+ get '/defined_in' do
54
+ { "declared_params" => declared(params) }
55
+ end
56
+
57
+ desc 'full set of request param types using `:in`',
58
+ success: TheApi::Entities::UseResponse
59
+ params do
60
+ requires :in_path, type: Integer
61
+ optional :in_query, type: String, documentation: { in: 'query' }
62
+ optional :in_header, type: String, documentation: { in: 'header' }
63
+ end
64
+
65
+ get '/defined_in/:in_path' do
66
+ { "declared_params" => declared(params) }
67
+ end
68
+
69
+ desc 'full set of request param types using `:in`'
70
+ params do
71
+ optional :in_path, type: Integer
72
+ optional :in_query, type: String, documentation: { in: 'query' }
73
+ optional :in_header, type: String, documentation: { in: 'header' }
74
+ end
75
+
76
+ delete '/defined_in/:in_path' do
77
+ { "declared_params" => declared(params) }
78
+ end
79
+
80
+ # file
81
+ desc 'file download',
82
+ success: TheApi::Entities::UseResponse
83
+ params do
84
+ requires :name, type: String
85
+ end
86
+
87
+ get '/download' do
88
+ { "declared_params" => declared(params) }
89
+ end
90
+
91
+ desc 'file upload',
92
+ success: TheApi::Entities::UseResponse
93
+ params do
94
+ requires :name, type: File
95
+ end
96
+
97
+ post '/upload' do
98
+ { "declared_params" => declared(params) }
99
+ end
100
+
101
+
102
+ add_swagger_documentation
103
+ end
104
+ end
105
+ end
106
+
107
+ def app
108
+ TheApi::ParamTypeApi
109
+ end
110
+
111
+ describe 'foo' do
112
+ subject do
113
+ get '/swagger_doc'
114
+ JSON.parse(last_response.body)
115
+ end
116
+
117
+ specify do
118
+ expect(subject['paths']['/defined_param_type/{in_path}']['delete']['responses']).to eql({
119
+ "200"=>{"description"=>"full set of request param types", "schema"=>{"$ref"=>"#/definitions/UseResponse"}}
120
+ })
121
+ end
122
+
123
+ specify do
124
+ expect(subject['paths']['/defined_in/{in_path}']['delete']['responses']).to eql({
125
+ "204"=>{"description"=>"full set of request param types using `:in`"}
126
+ })
127
+ end
128
+ end
129
+
130
+ describe 'defined param types' do
131
+ subject do
132
+ get '/swagger_doc/defined_param_type'
133
+ JSON.parse(last_response.body)
134
+ end
135
+
136
+ specify do
137
+ expect(subject['paths']['/defined_param_type']['get']['parameters']).to eql([
138
+ {"in"=>"query", "name"=>"in_query", "description"=>nil, "required"=>false, "type"=>"string"},
139
+ {"in"=>"header", "name"=>"in_header", "description"=>nil, "required"=>false, "type"=>"string"},
140
+ ])
141
+ end
142
+
143
+ specify do
144
+ expect(subject['paths']['/defined_param_type/{in_path}']['get']['parameters']).to eql([
145
+ {"in"=>"path", "name"=>"in_path", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"},
146
+ {"in"=>"query", "name"=>"in_query", "description"=>nil, "required"=>false, "type"=>"string"},
147
+ {"in"=>"header", "name"=>"in_header", "description"=>nil, "required"=>false, "type"=>"string"},
148
+ ])
149
+ end
150
+
151
+ specify do
152
+ expect(subject['paths']['/defined_param_type/{in_path}']['delete']['parameters']).to eql([
153
+ {"in"=>"path", "name"=>"in_path", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"},
154
+ {"in"=>"query", "name"=>"in_query", "description"=>nil, "required"=>false, "type"=>"string"},
155
+ {"in"=>"header", "name"=>"in_header", "description"=>nil, "required"=>false, "type"=>"string"},
156
+ ])
157
+ end
158
+ end
159
+
160
+ describe 'defined param types with `:in`' do
161
+ subject do
162
+ get '/swagger_doc/defined_in'
163
+ JSON.parse(last_response.body)
164
+ end
165
+
166
+ specify do
167
+ expect(subject['paths']['/defined_in']['get']['parameters']).to eql([
168
+ {"in"=>"query", "name"=>"in_query", "description"=>nil, "required"=>false, "type"=>"string"},
169
+ {"in"=>"header", "name"=>"in_header", "description"=>nil, "required"=>false, "type"=>"string"},
170
+ ])
171
+ end
172
+
173
+ specify do
174
+ expect(subject['paths']['/defined_in/{in_path}']['get']['parameters']).to eql([
175
+ {"in"=>"path", "name"=>"in_path", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"},
176
+ {"in"=>"query", "name"=>"in_query", "description"=>nil, "required"=>false, "type"=>"string"},
177
+ {"in"=>"header", "name"=>"in_header", "description"=>nil, "required"=>false, "type"=>"string"},
178
+ ])
179
+ end
180
+
181
+ specify do
182
+ expect(subject['paths']['/defined_in/{in_path}']['delete']['parameters']).to eql([
183
+ {"in"=>"path", "name"=>"in_path", "description"=>nil, "required"=>true, "type"=>"integer", "format"=>"int32"},
184
+ {"in"=>"query", "name"=>"in_query", "description"=>nil, "required"=>false, "type"=>"string"},
185
+ {"in"=>"header", "name"=>"in_header", "description"=>nil, "required"=>false, "type"=>"string"},
186
+ ])
187
+ end
188
+ end
189
+
190
+ describe 'file' do
191
+ describe 'upload' do
192
+ subject do
193
+ get '/swagger_doc/upload'
194
+ JSON.parse(last_response.body)
195
+ end
196
+
197
+ specify do
198
+ expect(subject['paths']['/upload']['post']['parameters']).to eql([
199
+ {"in"=>"formData", "name"=>"name", "description"=>nil, "required"=>true, "type"=>"file"}
200
+ ])
201
+ end
202
+ end
203
+
204
+ describe 'download' do
205
+ subject do
206
+ get '/swagger_doc/download'
207
+ JSON.parse(last_response.body)
208
+ end
209
+
210
+ specify do
211
+ expect(subject['paths']['/download']['get']['parameters']).to eql([
212
+ {"in"=>"query", "name"=>"name", "description"=>nil, "required"=>true, "type"=>"string"}
213
+ ])
214
+ end
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'additional parameter settings' do
4
+ before :all do
5
+ module TheApi
6
+ class RequestParamFix < Grape::API
7
+ resource :bookings do
8
+ desc 'Update booking'
9
+ params do
10
+ optional :name, type: String
11
+ end
12
+ put ':id' do
13
+ { "declared_params" => declared(params) }
14
+ end
15
+
16
+ desc 'Get booking details'
17
+ get ':id' do
18
+ { "declared_params" => declared(params) }
19
+ end
20
+
21
+ desc 'Get booking details by access_number'
22
+ get '/conf/:access_number' do
23
+ { "declared_params" => declared(params) }
24
+ end
25
+
26
+ desc 'Remove booking'
27
+ delete ':id' do
28
+ { "declared_params" => declared(params) }
29
+ end
30
+ end
31
+
32
+ add_swagger_documentation
33
+ end
34
+ end
35
+ end
36
+
37
+ def app
38
+ TheApi::RequestParamFix
39
+ end
40
+
41
+ subject do
42
+ get '/swagger_doc'
43
+ JSON.parse(last_response.body)
44
+ end
45
+
46
+ specify do
47
+ expect(subject['paths']['/bookings/{id}']['put']['parameters']).to eql([
48
+ {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true},
49
+ {"in"=>"formData", "name"=>"name", "description"=>nil, "type"=>"string", "required"=>false}
50
+ ])
51
+ end
52
+
53
+ specify do
54
+ expect(subject['paths']['/bookings/{id}']['get']['parameters']).to eql([
55
+ {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true}
56
+ ])
57
+ end
58
+
59
+ specify do
60
+ expect(subject['paths']['/bookings/{id}']['delete']['parameters']).to eql([
61
+ {"in"=>"path", "name"=>"id", "description"=>nil, "type"=>"integer", "format"=>"int32", "required"=>true}
62
+ ])
63
+ end
64
+ end
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'response' do
4
+ include_context "the api entities"
5
+
6
+ before :all do
7
+ module TheApi
8
+ class ResponseApi < Grape::API
9
+ format :json
10
+
11
+ desc 'This returns something',
12
+ params: Entities::UseResponse.documentation,
13
+ failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}]
14
+ post '/params_response' do
15
+ { "declared_params" => declared(params) }
16
+ end
17
+
18
+ desc 'This returns something',
19
+ entity: Entities::UseResponse,
20
+ failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}]
21
+ get '/entity_response' do
22
+ { "declared_params" => declared(params) }
23
+ end
24
+
25
+ desc 'This returns something',
26
+ entity: Entities::UseItemResponseAsType,
27
+ failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}]
28
+ get '/nested_type' do
29
+ { "declared_params" => declared(params) }
30
+ end
31
+
32
+
33
+ add_swagger_documentation
34
+ end
35
+ end
36
+ end
37
+
38
+ def app
39
+ TheApi::ResponseApi
40
+ end
41
+
42
+ describe "uses nested type as response object" do
43
+ subject do
44
+ get '/swagger_doc/nested_type'
45
+ JSON.parse(last_response.body)
46
+ end
47
+ specify do
48
+ expect(subject).to eql({
49
+ "info"=>{"title"=>"API title", "version"=>"v1"},
50
+ "swagger"=>"2.0",
51
+ "produces"=>["application/json"],
52
+ "host"=>"example.org",
53
+ "tags"=>[
54
+ {"name"=>"params_response", "description"=>"Operations about params_responses"},
55
+ {"name"=>"entity_response", "description"=>"Operations about entity_responses"},
56
+ {"name"=>"nested_type", "description"=>"Operations about nested_types"}
57
+ ],
58
+ "schemes"=>["https", "http"],
59
+ "paths"=>{
60
+ "/nested_type"=>{
61
+ "get"=>{
62
+ "description"=>"This returns something",
63
+ "produces"=>["application/json"],
64
+ "responses"=>{
65
+ "200"=>{"description"=>"This returns something", "schema"=>{"$ref"=>"#/definitions/UseItemResponseAsType"}},
66
+ "400"=>{"description"=>"NotFound", "schema"=>{"$ref"=>"#/definitions/ApiError"}}
67
+ },
68
+ "tags"=>["nested_type"],
69
+ "operationId"=>"getNestedType"
70
+ }}},
71
+ "definitions"=>{
72
+ "ResponseItem"=>{
73
+ "type"=>"object",
74
+ "properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}, "name"=>{"type"=>"string"}}
75
+ },
76
+ "UseItemResponseAsType"=>{
77
+ "type"=>"object",
78
+ "properties"=>{"description"=>{"type"=>"string"}, "responses"=>{"$ref"=>"#/definitions/ResponseItem"}},
79
+ "description"=>"This returns something"
80
+ },
81
+ "ApiError"=>{
82
+ "type"=>"object",
83
+ "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}},
84
+ "description"=>"This returns something"
85
+ }}
86
+ })
87
+ end
88
+ end
89
+
90
+ describe "uses entity as response object" do
91
+ subject do
92
+ get '/swagger_doc/entity_response'
93
+ JSON.parse(last_response.body)
94
+ end
95
+
96
+ specify do
97
+ expect(subject).to eql({
98
+ "info"=>{"title"=>"API title", "version"=>"v1"},
99
+ "swagger"=>"2.0",
100
+ "produces"=>["application/json"],
101
+ "host"=>"example.org",
102
+ "tags" => [
103
+ {"name"=>"params_response", "description"=>"Operations about params_responses"},
104
+ {"name"=>"entity_response", "description"=>"Operations about entity_responses"},
105
+ {"name"=>"nested_type", "description"=>"Operations about nested_types"}
106
+ ],
107
+ "schemes"=>["https", "http"],
108
+ "paths"=>{
109
+ "/entity_response"=>{
110
+ "get"=>{
111
+ "description"=>"This returns something",
112
+ "produces"=>["application/json"],
113
+ "tags"=>["entity_response"],
114
+ "operationId"=>"getEntityResponse",
115
+ "responses"=>{
116
+ "200"=>{"description"=>"This returns something", "schema"=>{"$ref"=>"#/definitions/UseResponse"}},
117
+ "400"=>{"description"=>"NotFound", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}}}},
118
+ "definitions"=>{
119
+ "ResponseItem"=>{
120
+ "type"=>"object",
121
+ "properties"=>{"id"=>{"type"=>"integer", "format"=>"int32"}, "name"=>{"type"=>"string"}}},
122
+ "UseResponse"=>{
123
+ "type"=>"object",
124
+ "properties"=>{"description"=>{"type"=>"string"}, "$responses"=>{"type"=>"array", "items"=>{"$ref"=>"#/definitions/ResponseItem"}}},
125
+ "description"=>"This returns something"
126
+ },
127
+ "ApiError"=>{
128
+ "type"=>"object",
129
+ "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}},
130
+ "description"=>"This returns something"
131
+ }
132
+ }})
133
+ end
134
+ end
135
+
136
+ describe "uses params as response object" do
137
+ subject do
138
+ get '/swagger_doc/params_response'
139
+ JSON.parse(last_response.body)
140
+ end
141
+
142
+ specify do
143
+ expect(subject).to eql({
144
+ "info"=>{"title"=>"API title", "version"=>"v1"},
145
+ "swagger"=>"2.0",
146
+ "produces"=>["application/json"],
147
+ "host"=>"example.org",
148
+ "tags" => [
149
+ {"name"=>"params_response", "description"=>"Operations about params_responses"},
150
+ {"name"=>"entity_response", "description"=>"Operations about entity_responses"},
151
+ {"name"=>"nested_type", "description"=>"Operations about nested_types"}
152
+ ],
153
+ "schemes"=>["https", "http"],
154
+ "paths"=>{
155
+ "/params_response"=>{
156
+ "post"=>{
157
+ "description"=>"This returns something",
158
+ "produces"=>["application/json"],
159
+ "consumes"=>["application/json"],
160
+ "parameters"=>[
161
+ {"in"=>"formData", "name"=>"description", "description"=>nil, "type"=>"string", "required"=>false},
162
+ {"in"=>"formData", "name"=>"$responses", "description"=>nil, "required"=>false, "type"=>"array", "items"=>{"type"=>"string"}}],
163
+ "tags"=>["params_response"],
164
+ "operationId"=>"postParamsResponse",
165
+ "responses"=>{
166
+ "201"=>{"description"=>"This returns something", "schema"=>{"$ref"=>"#/definitions/ParamsResponse"}},
167
+ "400"=>{"description"=>"NotFound", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}
168
+ }}},
169
+ "definitions"=>{
170
+ "ParamsResponse"=>{
171
+ "type"=>"object",
172
+ "properties"=>{"description"=>{"type"=>"string"}, "$responses"=>{"type"=>"string"}},
173
+ "description"=>"This returns something"
174
+ },
175
+ "ApiError"=>{
176
+ "type"=>"object",
177
+ "properties"=>{"code"=>{"type"=>"integer", "format"=>"int32"}, "message"=>{"type"=>"string"}},
178
+ "description"=>"This returns something"
179
+ }
180
+ }})
181
+ end
182
+ end
183
+
184
+ end