Skip to content

Commit ae0260c

Browse files
committed
Fixed broken description logic from PR #23, added back in early return
1 parent ebb2fcd commit ae0260c

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

Annotations/AnnotationGenerator.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function getResponseInfoFromDocBlock(string $docBlock): array
357357
$responseInfo['description'] = 'Response of unknown type';
358358
}
359359
if (!empty($returnTag->getDescription())) {
360-
$responseInfo['description'] = $returnTag->getDescription();
360+
$responseInfo['description'] = $this->getDescriptionText($returnTag->getDescription());
361361
}
362362

363363
return $responseInfo;
@@ -547,6 +547,22 @@ protected function normaliseDescriptionText(string $description): string
547547
return str_replace('"', '""', $description);
548548
}
549549

550+
/**
551+
* Normalise phpDocumentor description values into plain strings.
552+
*
553+
* @param mixed $description
554+
*
555+
* @return string
556+
*/
557+
protected function getDescriptionText($description): string
558+
{
559+
if ($description instanceof Description) {
560+
return $description->getBodyTemplate();
561+
}
562+
563+
return is_string($description) ? $description : '';
564+
}
565+
550566
/**
551567
* Add an entry to the map of warnings about missing important information, like type and description of parameters
552568
* and returns.
@@ -1149,10 +1165,7 @@ protected function determineResponses(array $rules, string $plugin, string $meth
11491165
$successArray['ref'] = '#/components/responses/GenericSuccess';
11501166
}
11511167

1152-
$description = $responseInfo['description'] ?? null;
1153-
if ($description instanceof Description) {
1154-
$description = $description->getBodyTemplate();
1155-
}
1168+
$description = $this->getDescriptionText($responseInfo['description'] ?? null);
11561169

11571170
// If it's a generic type and there's no custom description, use one of the global generic responses
11581171
if (empty($successArray['ref']) && !empty($responseInfo['type']) && empty($description)) {
@@ -1927,17 +1940,19 @@ public function compileOperationLines(string $path, string $opId, string $plugin
19271940
$operationValuesMap[] = ['@OA\Parameter' => $paramMap];
19281941
}
19291942
foreach ($responses as $response) {
1943+
$responseDescription = $this->getDescriptionText($response['description'] ?? null);
1944+
19301945
// Don't use the reference if there are media type examples
19311946
if (isset($response['ref']) && empty($response['mediaTypes'])) {
19321947
$code = $response['code'];
19331948
$codeFormatted = is_numeric($code) ? (string)$code : '"' . $code . '"';
1934-
$description = !empty($response['description']) && strpos($response['description'], 'Example links: [') !== false
1935-
? ', description="' . $this->normaliseDescriptionText($response['description']) . '"' : '';
1949+
$description = $responseDescription !== '' && strpos($responseDescription, 'Example links: [') !== false
1950+
? ', description="' . $this->normaliseDescriptionText($responseDescription) . '"' : '';
19361951
$operationValuesMap[] = '@OA\Response(response=' . $codeFormatted . $description . ', ref="' . $response['ref'] . '")';
19371952
} else {
19381953
$responsePropertyArray = [
19391954
'response=200',
1940-
'description="' . $this->normaliseDescriptionText($response['description'] ?? 'OK') . '"',
1955+
'description="' . $this->normaliseDescriptionText($responseDescription !== '' ? $responseDescription : 'OK') . '"',
19411956
];
19421957
if (!empty($response['schema'])) {
19431958
$responsePropertyArray = array_merge($responsePropertyArray, $response['schema']);

Commands/GenerateSpecFile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ protected function doExecute(): int
127127

128128
if ($notDryRun) {
129129
$output->writeln('<info>Results written to plugins/OpenApiDocs/tmp/specs/ directory.</info>');
130+
return self::SUCCESS;
130131
}
131132

132133
$output->writeln($result);

0 commit comments

Comments
 (0)