Skip to content

Commit 6bac652

Browse files
blakenumbata
authored andcommitted
fix failing tests
1 parent ac90d9f commit 6bac652

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

lib/grape-swagger/openapi_3/endpoint.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,13 @@ def method_object(route, options, path)
116116
method = {}
117117
method[:summary] = summary_object(route)
118118
method[:description] = description_object(route)
119-
# method[:produces] = produces_object(route, options[:produces] || options[:format])
120119
# method[:consumes] = consumes_object(route, options[:format])
121120
method[:parameters] = params_object(route, options, path)
122121
method[:security] = security_object(route)
123-
method[:responses] = response_object(route)
122+
123+
produces = produces_object(route, options[:produces] || options[:format])
124+
125+
method[:responses] = response_object(route, produces)
124126
method[:tags] = route.options.fetch(:tags, tag_object(route, path))
125127
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
126128
method[:deprecated] = deprecated_object(route)
@@ -194,7 +196,7 @@ def params_object(route, options, path)
194196
parameters
195197
end
196198

197-
def response_object(route)
199+
def response_object(route, content_types)
198200
codes = http_codes_from_route(route)
199201
codes.map! { |x| x.is_a?(Array) ? { code: x[0], message: x[1], model: x[2], examples: x[3], headers: x[4] } : x }
200202

@@ -214,13 +216,23 @@ def response_object(route)
214216
value[:code] = 204
215217
end
216218

217-
next if value[:code] == 204
218-
next unless !response_model.start_with?('Swagger_doc') && (@definitions[response_model] || value[:model])
219+
next if value[:code] == 204 || value[:code] == 201
220+
221+
model = !response_model.start_with?('Swagger_doc') && (@definitions[response_model] || value[:model])
219222

220-
@definitions[response_model][:description] = description_object(route)
221223
ref = build_reference(route, value, response_model)
224+
memo[value[:code]][:content] = content_types.map do |c|
225+
if model
226+
[c, { schema: ref }]
227+
else
228+
[c, {}]
229+
end
230+
end.to_h
231+
232+
next unless model
233+
234+
@definitions[response_model][:description] = description_object(route)
222235

223-
memo[value[:code]][:content] = @content_types.map { |c| [c, { schema: ref }] }.to_h
224236
memo[value[:code]][:examples] = value[:examples] if value[:examples]
225237
end
226238
end

spec/openapi_3/simple_mounted_api_spec.rb

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def app
9090
'title' => 'API title', 'version' => '0.0.1'
9191
},
9292
'openapi' => '3.0.0',
93-
# 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
9493
'servers' => [
9594
'url' => 'http://example.org'
9695
],
@@ -107,66 +106,59 @@ def app
107106
'/' => {
108107
'get' => {
109108
'description' => 'Document root',
110-
'produces' => ['application/json'],
111-
'responses' => { '200' => { 'description' => 'Document root' } },
109+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'Document root' } },
112110
'operationId' => 'get'
113111
}
114112
},
115113
'/simple' => {
116114
'get' => {
117115
'description' => 'This gets something.',
118-
'produces' => ['application/json'],
119116
'tags' => ['simple'],
120117
'operationId' => 'getSimple',
121-
'responses' => { '200' => { 'description' => 'This gets something.' } }
118+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'This gets something.' } }
122119
}
123120
},
124121
'/simple-test' => {
125122
'get' => {
126123
'description' => 'This gets something for URL using - separator.',
127-
'produces' => ['application/json'],
128124
'tags' => ['simple-test'],
129125
'operationId' => 'getSimpleTest',
130-
'responses' => { '200' => { 'description' => 'This gets something for URL using - separator.' } }
126+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'This gets something for URL using - separator.' } }
131127
}
132128
},
133129
'/simple-head-test' => {
134130
'head' => {
135-
'produces' => ['application/json'],
136-
'responses' => { '200' => { 'description' => 'head SimpleHeadTest' } },
131+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'head SimpleHeadTest' } },
137132
'tags' => ['simple-head-test'],
138133
'operationId' => 'headSimpleHeadTest'
139134
}
140135
},
141136
'/simple-options-test' => {
142137
'options' => {
143-
'produces' => ['application/json'],
144-
'responses' => { '200' => { 'description' => 'option SimpleOptionsTest' } },
138+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'option SimpleOptionsTest' } },
145139
'tags' => ['simple-options-test'],
146140
'operationId' => 'optionsSimpleOptionsTest'
147141
}
148142
},
149143
'/simple_with_headers' => {
150144
'get' => {
151145
'description' => 'this gets something else',
152-
'produces' => ['application/json'],
153146
'parameters' => [
154147
{ 'in' => 'header', 'name' => 'XAuthToken', 'description' => 'A required header.', 'type' => 'string', 'required' => true },
155148
{ 'in' => 'header', 'name' => 'XOtherHeader', 'description' => 'An optional header.', 'type' => 'string', 'required' => false }
156149
],
157150
'tags' => ['simple_with_headers'],
158151
'operationId' => 'getSimpleWithHeaders',
159152
'responses' => {
160-
'200' => { 'description' => 'this gets something else' },
161-
'403' => { 'description' => 'invalid pony' },
162-
'405' => { 'description' => 'no ponies left!' }
153+
'200' => { 'content' => { 'application/json' => {} }, 'description' => 'this gets something else' },
154+
'403' => { 'content' => { 'application/json' => {} }, 'description' => 'invalid pony' },
155+
'405' => { 'content' => { 'application/json' => {} }, 'description' => 'no ponies left!' }
163156
}
164157
}
165158
},
166159
'/items' => {
167160
'post' => {
168161
'description' => 'this takes an array of parameters',
169-
'produces' => ['application/json'],
170162
'consumes' => ['application/json'],
171163
'parameters' => [{ 'in' => 'formData', 'name' => 'items[]', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'string' } }],
172164
'tags' => ['items'],
@@ -177,11 +169,10 @@ def app
177169
'/custom' => {
178170
'get' => {
179171
'description' => 'this uses a custom parameter',
180-
'produces' => ['application/json'],
181172
'parameters' => [{ 'in' => 'formData', 'name' => 'custom', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'CustomType' } }],
182173
'tags' => ['custom'],
183174
'operationId' => 'getCustom',
184-
'responses' => { '200' => { 'description' => 'this uses a custom parameter' } }
175+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'this uses a custom parameter' } }
185176
}
186177
}
187178
}
@@ -210,10 +201,9 @@ def app
210201
'/simple' => {
211202
'get' => {
212203
'description' => 'This gets something.',
213-
'produces' => ['application/json'],
214204
'tags' => ['simple'],
215205
'operationId' => 'getSimple',
216-
'responses' => { '200' => { 'description' => 'This gets something.' } }
206+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'This gets something.' } }
217207
}
218208
}
219209
}
@@ -234,7 +224,7 @@ def app
234224
'openapi' => '3.0.0',
235225
# 'produces' => ['application/xml', 'application/json', 'application/octet-stream', 'text/plain'],
236226
'servers' => [
237-
'url' => 'http://example.org'
227+
'url' => 'http://example.org'
238228
],
239229
'tags' => [
240230
{ 'name' => 'simple-test', 'description' => 'Operations about simple-tests' }
@@ -243,10 +233,9 @@ def app
243233
'/simple-test' => {
244234
'get' => {
245235
'description' => 'This gets something for URL using - separator.',
246-
'produces' => ['application/json'],
247236
'tags' => ['simple-test'],
248237
'operationId' => 'getSimpleTest',
249-
'responses' => { '200' => { 'description' => 'This gets something for URL using - separator.' } }
238+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'This gets something for URL using - separator.' } }
250239
}
251240
}
252241
}
@@ -265,17 +254,16 @@ def app
265254
'/simple_with_headers' => {
266255
'get' => {
267256
'description' => 'this gets something else',
268-
'produces' => ['application/json'],
269257
'parameters' => [
270258
{ 'in' => 'header', 'name' => 'XAuthToken', 'description' => 'A required header.', 'type' => 'string', 'required' => true },
271259
{ 'in' => 'header', 'name' => 'XOtherHeader', 'description' => 'An optional header.', 'type' => 'string', 'required' => false }
272260
],
273261
'tags' => ['simple_with_headers'],
274262
'operationId' => 'getSimpleWithHeaders',
275263
'responses' => {
276-
'200' => { 'description' => 'this gets something else' },
277-
'403' => { 'description' => 'invalid pony' },
278-
'405' => { 'description' => 'no ponies left!' }
264+
'200' => { 'content' => { 'application/json' => {} }, 'description' => 'this gets something else' },
265+
'403' => { 'content' => { 'application/json' => {} }, 'description' => 'invalid pony' },
266+
'405' => { 'content' => { 'application/json' => {} }, 'description' => 'no ponies left!' }
279267
}
280268
}
281269
}
@@ -294,7 +282,6 @@ def app
294282
'/items' => {
295283
'post' => {
296284
'description' => 'this takes an array of parameters',
297-
'produces' => ['application/json'],
298285
'consumes' => ['application/json'],
299286
'parameters' => [{ 'in' => 'formData', 'name' => 'items[]', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'string' } }],
300287
'tags' => ['items'],
@@ -317,11 +304,10 @@ def app
317304
'/custom' => {
318305
'get' => {
319306
'description' => 'this uses a custom parameter',
320-
'produces' => ['application/json'],
321307
'parameters' => [{ 'in' => 'formData', 'name' => 'custom', 'description' => 'array of items', 'required' => false, 'type' => 'array', 'items' => { 'type' => 'CustomType' } }],
322308
'tags' => ['custom'],
323309
'operationId' => 'getCustom',
324-
'responses' => { '200' => { 'description' => 'this uses a custom parameter' } }
310+
'responses' => { '200' => { 'content' => { 'application/json' => {} }, 'description' => 'this uses a custom parameter' } }
325311
}
326312
}
327313
)

0 commit comments

Comments
 (0)