Skip to content

Commit

Permalink
Allow multiple content types and schemas (via oneOf) according to Ope…
Browse files Browse the repository at this point in the history
…nAPI 3.0 spec (#792)

* rewrite generateEndpointResponsesSpec to allow multiple content types and schemas (via oneOf) according to OpenAPI 3.0 spec

* Update OpenAPISpecWriter.php

* Add test

---------

Co-authored-by: Shalvah <[email protected]>
  • Loading branch information
rhurling and shalvah authored Jun 17, 2024
1 parent a1cb38e commit bd28127
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,32 @@ protected function generateEndpointResponsesSpec(OutputEndpointData $endpoint)
$responses[204] = [
'description' => $this->getResponseDescription($response),
];
} elseif (isset($responses[$response->status])) {
// If we already have a response for this status code and content type,
// we change to a `oneOf` which includes all the responses
$content = $this->generateResponseContentSpec($response->content, $endpoint);
$contentType = array_keys($content)[0];
if (isset($responses[$response->status]['content'][$contentType])) {
// If we've already created the oneOf object, add this response
if (isset($responses[$response->status]['content'][$contentType]['schema']['oneOf'])) {
$responses[$response->status]['content'][$contentType]['schema']['oneOf'][] = $content[$contentType];
} else {
// Create the oneOf object
$existingResponseExample = array_replace([
'description' => $responses[$response->status]['description'],
], $responses[$response->status]['content'][$contentType]['schema']);
$newResponseExample = array_replace([
'description' => $this->getResponseDescription($response),
], $content[$contentType]['schema']);

$responses[$response->status]['description'] = '';
$responses[$response->status]['content'][$contentType]['schema'] = [
'oneOf' => [$existingResponseExample, $newResponseExample]
];
}
}
} else {
// Store as the response for this status
$responses[$response->status] = [
'description' => $this->getResponseDescription($response),
'content' => $this->generateResponseContentSpec($response->content, $endpoint),
Expand Down
81 changes: 80 additions & 1 deletion tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public function adds_responses_correctly_as_responses_on_operation_object()
'type' => 'string',
'description' => 'Parameter description, ha!',
],
'sub level 0.sub level 1 key 3.sub level 2 key 1'=> [
'sub level 0.sub level 1 key 3.sub level 2 key 1' => [
'description' => 'This is description of nested object',
]
],
Expand Down Expand Up @@ -557,6 +557,85 @@ public function adds_responses_correctly_as_responses_on_operation_object()
], $results['paths']['/path2']['put']['responses']);
}

/** @test */
public function adds_multiple_responses_correctly_using_oneOf()
{
$endpointData1 = $this->createMockEndpointData([
'httpMethods' => ['POST'],
'uri' => '/path1',
'responses' => [
[
'status' => 201,
'description' => 'This one',
'content' => '{"this": "one"}',
],
[
'status' => 201,
'description' => 'No, that one.',
'content' => '{"that": "one"}',
],
[
'status' => 200,
'description' => 'A separate one',
'content' => '{"the other": "one"}',
],
],
]);
$groups = [$this->createGroup([$endpointData1])];

$results = $this->generate($groups);

$this->assertArraySubset([
'200' => [
'description' => 'A separate one',
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
'properties' => [
'the other' => [
'example' => "one",
'type' => 'string',
],
],
],
],
],
],
'201' => [
'description' => '',
'content' => [
'application/json' => [
'schema' => [
'oneOf' => [
[
'type' => 'object',
'description' => 'This one',
'properties' => [
'this' => [
'example' => "one",
'type' => 'string',
],
],
],
[
'type' => 'object',
'description' => 'No, that one.',
'properties' => [
'that' => [
'example' => "one",
'type' => 'string',
],
],
],
],
],
],
],
],
], $results['paths']['/path1']['post']['responses']);
}

protected function createMockEndpointData(array $custom = []): OutputEndpointData
{
$faker = Factory::create();
Expand Down

0 comments on commit bd28127

Please sign in to comment.