Skip to content

Commit 68bd434

Browse files
committed
added readable operationIds
1 parent 73752f0 commit 68bd434

2 files changed

Lines changed: 55 additions & 10 deletions

File tree

app/helpers/Swagger/AnnotationData.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@
22

33
namespace App\Helpers\Swagger;
44

5+
use App\Helpers\MetaFormats\AnnotationConversion\Utils;
6+
57
/**
68
* A data structure for endpoint signatures that can produce annotations parsable by a swagger generator.
79
*/
810
class AnnotationData
911
{
1012
public HttpMethods $httpMethod;
1113

14+
public string $className;
15+
public string $methodName;
1216
public array $pathParams;
1317
public array $queryParams;
1418
public array $bodyParams;
1519
public ?string $endpointDescription;
1620

1721
public function __construct(
22+
string $className,
23+
string $methodName,
1824
HttpMethods $httpMethod,
1925
array $pathParams,
2026
array $queryParams,
2127
array $bodyParams,
22-
string $endpointDescription = null,
28+
?string $endpointDescription = null,
2329
) {
30+
$this->className = $className;
31+
$this->methodName = $methodName;
2432
$this->httpMethod = $httpMethod;
2533
$this->pathParams = $pathParams;
2634
$this->queryParams = $queryParams;
@@ -68,16 +76,31 @@ private function getBodyAnnotation(): string | null
6876
return $head . $body->toString() . "))";
6977
}
7078

71-
/**
72-
* Converts the extracted annotation data to a string parsable by the Swagger-PHP library.
73-
* @param string $route The route of the handler this set of data represents.
74-
* @return string Returns the transpiled annotations on a single line.
75-
*/
79+
/**
80+
* Constructs an operation ID used to identify the endpoint.
81+
* The operation ID is composed of the presenter class name and the endpoint method name with the 'action' prefix.
82+
* @return string Returns the operation ID.
83+
*/
84+
private function constructOperationId()
85+
{
86+
// remove the namespace prefix of the class and make the first letter lowercase
87+
$className = lcfirst(Utils::shortenClass($this->className));
88+
// remove the 'action' prefix
89+
$endpoint = substr($this->methodName, strlen("action"));
90+
return $className . $endpoint;
91+
}
92+
93+
/**
94+
* Converts the extracted annotation data to a string parsable by the Swagger-PHP library.
95+
* @param string $route The route of the handler this set of data represents.
96+
* @return string Returns the transpiled annotations on a single line.
97+
*/
7698
public function toSwaggerAnnotations(string $route)
7799
{
78100
$httpMethodAnnotation = $this->getHttpMethodAnnotation();
79101
$body = new ParenthesesBuilder();
80102
$body->addKeyValue("path", $route);
103+
$body->addKeyValue("operationId", $this->constructOperationId());
81104

82105
// add the endpoint description when provided
83106
if ($this->endpointDescription !== null) {

app/helpers/Swagger/AnnotationHelper.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ private static function extractAnnotationDescription(array $annotations): ?strin
279279
}
280280

281281
private static function annotationParameterDataToAnnotationData(
282-
HttpMethods $method,
282+
string $className,
283+
string $methodName,
284+
HttpMethods $httpMethod,
283285
array $params,
284286
?string $description
285287
): AnnotationData {
@@ -299,7 +301,15 @@ private static function annotationParameterDataToAnnotationData(
299301
}
300302
}
301303

302-
return new AnnotationData($method, $pathParams, $queryParams, $bodyParams, $description);
304+
return new AnnotationData(
305+
$className,
306+
$methodName,
307+
$httpMethod,
308+
$pathParams,
309+
$queryParams,
310+
$bodyParams,
311+
$description
312+
);
303313
}
304314

305315
/**
@@ -323,7 +333,13 @@ public static function extractStandardAnnotationData(
323333
$params = self::extractStandardAnnotationParams($methodAnnotations, $route);
324334
$description = self::extractAnnotationDescription($methodAnnotations);
325335

326-
return self::annotationParameterDataToAnnotationData($httpMethod, $params, $description);
336+
return self::annotationParameterDataToAnnotationData(
337+
$className,
338+
$methodName,
339+
$httpMethod,
340+
$params,
341+
$description
342+
);
327343
}
328344

329345
/**
@@ -356,7 +372,13 @@ public static function extractAttributeData(string $className, string $methodNam
356372
}, $attributeData);
357373
$description = self::extractAnnotationDescription($methodAnnotations);
358374

359-
return self::annotationParameterDataToAnnotationData($httpMethod, $params, $description);
375+
return self::annotationParameterDataToAnnotationData(
376+
$className,
377+
$methodName,
378+
$httpMethod,
379+
$params,
380+
$description
381+
);
360382
}
361383

362384
/**

0 commit comments

Comments
 (0)