Skip to content

Commit

Permalink
rewrite generateEndpointResponsesSpec to allow multiple content types…
Browse files Browse the repository at this point in the history
… and schemas (via oneOf) according to OpenAPI 3.0 spec
  • Loading branch information
rhurling authored Jan 24, 2024
1 parent cb258f6 commit 10f4c43
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,34 @@ protected function generateEndpointResponsesSpec(OutputEndpointData $endpoint)

foreach ($endpoint->responses as $response) {
// OpenAPI groups responses by status code
// Only one response type per status code, so only the last one will be used
// OpenAPI 3.0 allows multiple different response content types and schemas per content type (via oneOf), so all responses will be used and merged (but not deduplicated)
// Only the first response per status code sets the overall description of the response status code
if (intval($response->status) === 204) {
// Must not add content for 204
$responses[204] = [
'description' => $this->getResponseDescription($response),
];
} elseif (isset($responses[$response->status])) {
$content = $this->generateResponseContentSpec($response->content, $endpoint);
$contentType = array_keys($content)[0];
if (isset($responses[$response->status]['content'][$contentType])) {
if (!isset($responses[$response->status]['content'][$contentType]['schema']['oneOf'])) {
$oldSchema = array_replace([
'description' => $responses[$response->status]['description'],
], $responses[$response->status]['content'][$contentType]['schema']);

$responses[$response->status]['content'][$contentType]['schema'] = [
'oneOf' => [$newSchema],

Check failure on line 283 in src/Writing/OpenAPISpecWriter.php

View workflow job for this annotation

GitHub Actions / Lint code (PHP 8.1)

Undefined variable: $newSchema
];
}
$newSchema = array_replace([
'description' => $this->getResponseDescription($response),
], $content[$contentType]['schema']);

$responses[$response->status]['content'][$contentType]['schema']['oneOf'][] = $newSchema;
} else {
$responses[$response->status]['content'][$contentType] = $content[$contentType];
}
} else {
$responses[$response->status] = [
'description' => $this->getResponseDescription($response),
Expand Down

0 comments on commit 10f4c43

Please sign in to comment.