Skip to content

Commit 654339e

Browse files
fix(openapi): Improve response override (#7428)
1 parent 55fd657 commit 654339e

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,15 @@ private function buildOpenApiResponse(array $existingResponses, int|string $stat
504504
$noOutput = \is_array($operation?->getOutput()) && null === $operation->getOutput()['class'];
505505

506506
$response = $existingResponses[$status] ?? new Response($description);
507+
if (null === $response->getDescription()) {
508+
$response = $response->withDescription($description);
509+
}
507510

508-
if (!$response->getContent() && $responseMimeTypes && $operationOutputSchemas && !$noOutput) {
511+
if (null === $response->getContent() && $responseMimeTypes && $operationOutputSchemas && !$noOutput) {
509512
$response = $response->withContent($this->buildContent($responseMimeTypes, $operationOutputSchemas));
510513
}
511514

512-
if (!$response->getLinks() && $resourceMetadataCollection && $operation) {
515+
if (null === $response->getLinks() && $resourceMetadataCollection && $operation) {
513516
$response = $response->withLinks($this->getLinks($resourceMetadataCollection, $operation));
514517
}
515518

src/OpenApi/Model/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ final class Response
1717
{
1818
use ExtensionTrait;
1919

20-
public function __construct(private string $description = '', private ?\ArrayObject $content = null, private ?\ArrayObject $headers = null, private ?\ArrayObject $links = null)
20+
public function __construct(private ?string $description = null, private ?\ArrayObject $content = null, private ?\ArrayObject $headers = null, private ?\ArrayObject $links = null)
2121
{
2222
}
2323

24-
public function getDescription(): string
24+
public function getDescription(): ?string
2525
{
2626
return $this->description;
2727
}

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ public function testInvoke(): void
262262
),
263263
],
264264
)),
265+
'postDummyItemWithEmptyResponse' => (new Post())->withUriTemplate('/dummyitems/noresponse')->withOperation($baseOperation)->withOpenapi(new OpenApiOperation(
266+
responses: [
267+
'201' => new OpenApiResponse(
268+
description: '',
269+
content: new \ArrayObject(),
270+
headers: new \ArrayObject(),
271+
links: new \ArrayObject(),
272+
),
273+
'400' => new OpenApiResponse(),
274+
],
275+
requestBody: new RequestBody(''),
276+
)),
265277
'postDummyItemWithoutInput' => (new Post())->withUriTemplate('/dummyitem/noinput')->withOperation($baseOperation)->withInput(false),
266278
'getDummyCollectionWithErrors' => (new GetCollection())->withUriTemplate('erroredDummies')->withErrors([DummyErrorResource::class])->withOperation($baseOperation),
267279
])
@@ -1209,6 +1221,40 @@ public function testInvoke(): void
12091221
]
12101222
), $dummyItemPath->getGet());
12111223

1224+
$emptyReponsePath = $paths->getPath('/dummyitems/noresponse');
1225+
$this->assertEquals(new Operation(
1226+
'postDummyItemWithEmptyResponse',
1227+
['Dummy'],
1228+
[
1229+
'201' => new Response(
1230+
'',
1231+
new \ArrayObject(),
1232+
new \ArrayObject(),
1233+
new \ArrayObject()
1234+
),
1235+
'400' => new Response(
1236+
'Invalid input',
1237+
content: new \ArrayObject(['application/problem+json' => new MediaType(schema: new \ArrayObject(['$ref' => '#/components/schemas/Error']))]),
1238+
links: new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'This is a dummy')])
1239+
),
1240+
'422' => new Response(
1241+
'Unprocessable entity',
1242+
content: new \ArrayObject(['application/problem+json' => new MediaType(schema: new \ArrayObject(['$ref' => '#/components/schemas/Error']))]),
1243+
links: new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'This is a dummy')])
1244+
),
1245+
],
1246+
'Creates a Dummy resource.',
1247+
'Creates a Dummy resource.',
1248+
null,
1249+
[],
1250+
new RequestBody(
1251+
'',
1252+
content: new \ArrayObject([
1253+
'application/ld+json' => new MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy.jsonld'])),
1254+
]),
1255+
),
1256+
), $emptyReponsePath->getPost());
1257+
12121258
$emptyRequestBodyPath = $paths->getPath('/dummyitem/noinput');
12131259
$this->assertEquals(new Operation(
12141260
'postDummyItemWithoutInput',

0 commit comments

Comments
 (0)