Skip to content

Commit 80d21f1

Browse files
committed
Cast response codes to string for OpenAPI spec (closes #907, #979)
1 parent 821ed96 commit 80d21f1

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/Writing/OpenApiSpecGenerators/BaseGenerator.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,41 +229,42 @@ protected function generateEndpointResponsesSpec(OutputEndpointData $endpoint)
229229
$responses = [];
230230

231231
foreach ($endpoint->responses as $response) {
232+
$code = $response->status; // OpenAPI spec requires status codes to be integers
232233
// OpenAPI groups responses by status code
233234
// Only one response type per status code, so only the last one will be used
234-
if (intval($response->status) === 204) {
235+
if ($code === '204') {
235236
// Must not add content for 204
236-
$responses[204] = [
237+
$responses[$code] = [
237238
'description' => $this->getResponseDescription($response),
238239
];
239-
} elseif (isset($responses[$response->status])) {
240+
} elseif (isset($responses[$code])) {
240241
// If we already have a response for this status code and content type,
241242
// we change to a `oneOf` which includes all the responses
242243
$content = $this->generateResponseContentSpec($response->content, $endpoint);
243244
$contentType = array_keys($content)[0];
244-
if (isset($responses[$response->status]['content'][$contentType])) {
245+
if (isset($responses[$code]['content'][$contentType])) {
245246
$newResponseExample = array_replace([
246247
'description' => $this->getResponseDescription($response),
247248
], $content[$contentType]['schema']);
248249

249250
// If we've already created the oneOf object, add this response
250-
if (isset($responses[$response->status]['content'][$contentType]['schema']['oneOf'])) {
251-
$responses[$response->status]['content'][$contentType]['schema']['oneOf'][] = $newResponseExample;
251+
if (isset($responses[$code]['content'][$contentType]['schema']['oneOf'])) {
252+
$responses[$code]['content'][$contentType]['schema']['oneOf'][] = $newResponseExample;
252253
} else {
253254
// Create the oneOf object
254255
$existingResponseExample = array_replace([
255-
'description' => $responses[$response->status]['description'],
256-
], $responses[$response->status]['content'][$contentType]['schema']);
256+
'description' => $responses[$code]['description'],
257+
], $responses[$code]['content'][$contentType]['schema']);
257258

258-
$responses[$response->status]['description'] = '';
259-
$responses[$response->status]['content'][$contentType]['schema'] = [
259+
$responses[$code]['description'] = '';
260+
$responses[$code]['content'][$contentType]['schema'] = [
260261
'oneOf' => [$existingResponseExample, $newResponseExample]
261262
];
262263
}
263264
}
264265
} else {
265266
// Store as the response for this status
266-
$responses[$response->status] = [
267+
$responses[$code] = [
267268
'description' => $this->getResponseDescription($response),
268269
'content' => $this->generateResponseContentSpec($response->content, $endpoint),
269270
];

0 commit comments

Comments
 (0)