Skip to content

Commit 6e5be68

Browse files
authored
Merge pull request #19 from edersoares/multiple-responses
Allow multiple responses in paths
2 parents cd8e52a + edbc505 commit 6e5be68

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

src/Attributes/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Additionally, a schema type can be added (array or object) and a ref which will return any other property
1717
* Consider the ref parameter like a shortcut
1818
*/
19-
#[Attribute]
19+
#[Attribute(Attribute::TARGET_ALL | Attribute::IS_REPEATABLE)]
2020
class Response implements JsonSerializable
2121
{
2222
private ?Schema $schema = null;

src/Attributes/Route.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Route implements JsonSerializable
2323
public const PATCH = 'patch';
2424

2525
private array $getParams = [];
26-
private ?Response $response = null;
26+
private array $responses = [];
2727
private ?RequestBody $requestBody = null;
2828

2929
public function __construct(
@@ -40,9 +40,9 @@ public function addParam(Parameter $params): void
4040
$this->getParams[] = $params;
4141
}
4242

43-
public function setResponse(Response $response): void
43+
public function addResponse(Response $response): void
4444
{
45-
$this->response = $response;
45+
$this->responses[] = $response;
4646
}
4747

4848
public function getMethod(): string
@@ -106,8 +106,12 @@ public function jsonSerialize(): array
106106
$array[$this->getRoute()][$this->method]['requestBody'] = $this->requestBody;
107107
}
108108

109-
if ($this->response) {
110-
$array[$this->getRoute()][$this->method]['responses'] = $this->response;
109+
if ($this->responses) {
110+
$responses = array_reduce($this->responses, function ($response, $current) {
111+
return $response + $current->jsonSerialize();
112+
}, []);
113+
114+
$array[$this->getRoute()][$this->method]['responses'] = $responses;
111115
}
112116

113117
return $array;

src/PathMethodBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private function saveSchemaHolder(): void
122122
if ($this->currentSchemaHolder instanceof RequestBody) {
123123
$this->currentRoute->setRequestBody($this->currentSchemaHolder);
124124
} else {
125-
$this->currentRoute->setResponse($this->currentSchemaHolder);
125+
$this->currentRoute->addResponse($this->currentSchemaHolder);
126126
}
127127

128128
$this->currentSchemaHolder = null;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenApiGenerator\Tests\Examples\Controller;
6+
7+
use OpenApiGenerator\Attributes\Controller;
8+
use OpenApiGenerator\Attributes\GET;
9+
use OpenApiGenerator\Attributes\Response;
10+
11+
#[Controller]
12+
class ManyResponsesController
13+
{
14+
#[
15+
GET("/path", ["Dummy"], "Dummy path"),
16+
Response(200),
17+
Response(401),
18+
]
19+
public function get(): void
20+
{
21+
//
22+
}
23+
}

tests/GenerateHttpTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OpenApiGenerator\Attributes\Route;
1414
use OpenApiGenerator\Attributes\Schema;
1515
use OpenApiGenerator\GeneratorHttp;
16+
use OpenApiGenerator\Tests\Examples\Controller\ManyResponsesController;
1617
use OpenApiGenerator\Tests\Examples\Controller\SimpleController;
1718
use PHPUnit\Framework\TestCase;
1819
use ReflectionClass;
@@ -46,7 +47,27 @@ public function testAppend(): void
4647
$expectedRoute->addParam($expectedParameter);
4748
$expectedRoute->addParam($expectedPathParameter);
4849
$expectedRoute->setRequestBody($requestBody);
49-
$expectedRoute->setResponse(new Response());
50+
$expectedRoute->addResponse(new Response());
51+
52+
self::assertEquals([$expectedRoute], $actual);
53+
}
54+
55+
public function testManyResponses(): void
56+
{
57+
$dummyReflection = new ReflectionClass(ManyResponsesController::class);
58+
59+
$generateHttp = new GeneratorHttp();
60+
$generateHttp->append($dummyReflection);
61+
62+
$reflection = new ReflectionClass($generateHttp);
63+
$pathsProperty = $reflection->getProperty('paths');
64+
$pathsProperty->setAccessible(true);
65+
$actual = $pathsProperty->getValue($generateHttp);
66+
67+
$expectedRoute = new GET('/path', ['Dummy'], 'Dummy path');
68+
$expectedRoute->setRequestBody(new RequestBody());
69+
$expectedRoute->addResponse(new Response());
70+
$expectedRoute->addResponse(new Response(401));
5071

5172
self::assertEquals([$expectedRoute], $actual);
5273
}

0 commit comments

Comments
 (0)