Skip to content

Commit 3b0c441

Browse files
fix(openapi): ability to override description in response (#7412)
1 parent c9ed3f4 commit 3b0c441

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -496,22 +496,26 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
496496
}
497497
}
498498

499-
private function buildOpenApiResponse(array $existingResponses, int|string $status, string $description, ?Operation $openapiOperation = null, ?HttpOperation $operation = null, ?array $responseMimeTypes = null, ?array $operationOutputSchemas = null, ?ResourceMetadataCollection $resourceMetadataCollection = null): Operation
499+
/**
500+
* @param array<Response> $existingResponses
501+
*
502+
* @return Operation
503+
*/
504+
private function buildOpenApiResponse(array $existingResponses, int|string $status, string $description, Operation $openapiOperation, ?HttpOperation $operation = null, ?array $responseMimeTypes = null, ?array $operationOutputSchemas = null, ?ResourceMetadataCollection $resourceMetadataCollection = null): Operation
500505
{
501506
$noOutput = \is_array($operation?->getOutput()) && null === $operation->getOutput()['class'];
502507

503-
if (isset($existingResponses[$status])) {
504-
return $openapiOperation;
505-
}
506-
$responseLinks = $responseContent = null;
507-
if ($responseMimeTypes && $operationOutputSchemas && !$noOutput) {
508-
$responseContent = $this->buildContent($responseMimeTypes, $operationOutputSchemas);
508+
$response = $existingResponses[$status] ?? new Response($description);
509+
510+
if (!$response->getContent() && $responseMimeTypes && $operationOutputSchemas && !$noOutput) {
511+
$response = $response->withContent($this->buildContent($responseMimeTypes, $operationOutputSchemas));
509512
}
510-
if ($resourceMetadataCollection && $operation) {
511-
$responseLinks = $this->getLinks($resourceMetadataCollection, $operation);
513+
514+
if (!$response->getLinks() && $resourceMetadataCollection && $operation) {
515+
$response = $response->withLinks($this->getLinks($resourceMetadataCollection, $operation));
512516
}
513517

514-
return $openapiOperation->withResponse($status, new Response($description, $responseContent, null, $responseLinks));
518+
return $openapiOperation->withResponse($status, $response);
515519
}
516520

517521
/**

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public function testInvoke(): void
237237
responses: [
238238
'200' => new OpenApiResponse(
239239
description: 'Success',
240+
content: new \ArrayObject([]),
240241
),
241242
],
242243
)),
@@ -1106,7 +1107,11 @@ public function testInvoke(): void
11061107
'link' => ['$ref' => '#/components/schemas/Dummy'],
11071108
])
11081109
),
1109-
'400' => new Response('Error'),
1110+
'400' => new Response(
1111+
'Error',
1112+
content: new \ArrayObject(['application/problem+json' => new MediaType(schema: new \ArrayObject(['$ref' => '#/components/schemas/Error']))]),
1113+
links: new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'This is a dummy')])
1114+
),
11101115
'422' => new Response(
11111116
'Unprocessable entity',
11121117
content: new \ArrayObject(['application/problem+json' => new MediaType(schema: new \ArrayObject(['$ref' => '#/components/schemas/Error']))]),
@@ -1150,7 +1155,11 @@ public function testInvoke(): void
11501155
'link' => ['$ref' => '#/components/schemas/Dummy'],
11511156
])
11521157
),
1153-
'400' => new Response('Error'),
1158+
'400' => new Response(
1159+
'Error',
1160+
content: new \ArrayObject(['application/problem+json' => new MediaType(schema: new \ArrayObject(['$ref' => '#/components/schemas/Error']))]),
1161+
links: new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'This is a dummy')])
1162+
),
11541163
'422' => new Response(
11551164
'Unprocessable entity',
11561165
content: new \ArrayObject(['application/problem+json' => new MediaType(schema: new \ArrayObject(['$ref' => '#/components/schemas/Error']))]),
@@ -1177,7 +1186,8 @@ public function testInvoke(): void
11771186
['Dummy'],
11781187
[
11791188
'200' => new Response(
1180-
'Success'
1189+
'Success',
1190+
content: new \ArrayObject([]),
11811191
),
11821192
],
11831193
'Retrieves the collection of Dummy resources.',

0 commit comments

Comments
 (0)