Skip to content

Commit

Permalink
improve build operation id
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 22, 2024
1 parent 47f1553 commit 443d39e
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/Parser/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,22 @@ private function getTypeFromEnum(\ReflectionEnum $enum, string $name, bool $requ

public static function buildOperationId(string $controllerName, string $methodName): string
{
return str_replace('\\', '.', $controllerName . '.' . $methodName);
$result = [];
$parts = explode('\\', $controllerName);
array_shift($parts); // vendor
array_shift($parts); // controller

foreach ($parts as $part) {
$result[] = self::snakeCase($part);
}

$result[] = $methodName;

return implode('.', $result);
}

private static function snakeCase(string $name): string
{
return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $name));
}
}
2 changes: 1 addition & 1 deletion tests/Console/resource/spec_openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"foo"
],
"description": "A long **Test** description",
"operationId": "PSX.Api.Tests.Parser.Attribute.TestController.doGet",
"operationId": "test.get",
"parameters": [
{
"name": "foo",
Expand Down
1 change: 1 addition & 0 deletions tests/Parser/Attribute/BarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace PSX\Api\Tests\Parser\Attribute;

use PSX\Api\Attribute\Description;
use PSX\Api\Attribute\OperationId;
use PSX\Api\Attribute\Path;
use PSX\Api\Attribute\Post;
use PSX\Api\Tests\Parser\Model\Incoming;
Expand Down
2 changes: 2 additions & 0 deletions tests/Parser/Attribute/FooController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PSX\Api\Attribute\Description;
use PSX\Api\Attribute\Get;
use PSX\Api\Attribute\Incoming;
use PSX\Api\Attribute\OperationId;
use PSX\Api\Attribute\Outgoing;
use PSX\Api\Attribute\Path;

Expand All @@ -40,6 +41,7 @@ class FooController
#[Get]
#[Incoming(schema: '../schema/schema.json')]
#[Outgoing(code: 200, schema: '../schema/schema.json')]
#[OperationId('foo.get')]
protected function doGet()
{
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Parser/Attribute/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
use PSX\Api\Attribute\Description;
use PSX\Api\Attribute\Get;
use PSX\Api\Attribute\Incoming;
use PSX\Api\Attribute\OperationId;
use PSX\Api\Attribute\Outgoing;
use PSX\Api\Attribute\Path;
use PSX\Api\Attribute\PathParam;
use PSX\Api\Attribute\QueryParam;
use PSX\Api\Attribute\Tags;
use PSX\DateTime\DateTime;
use PSX\DateTime\LocalDateTime;
use PSX\Schema\Format;
use PSX\Schema\Type;

Expand Down Expand Up @@ -59,7 +60,8 @@ class TestController
#[Outgoing(code: 200, schema: __DIR__ . "/../schema/schema.json")]
#[Outgoing(code: 500, schema: __DIR__ . "/../schema/error.json")]
#[Tags(['foo'])]
protected function doGet(string $fooId, string $foo, string $bar, string $baz, string $boz, int $integer, float $number, DateTime $date, bool $boolean, string $string)
#[OperationId('test.get')]
protected function doGet(string $fooId, string $foo, string $bar, string $baz, string $boz, int $integer, float $number, LocalDateTime $date, bool $boolean, string $string)
{
}
}
4 changes: 2 additions & 2 deletions tests/Parser/AttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function getSpecification(): SpecificationInterface
public function testOperationId()
{
$specification = $this->apiManager->getApi(TestController::class);
$operation = $specification->getOperations()->get('PSX.Api.Tests.Parser.Attribute.TestController.doGet');
$operation = $specification->getOperations()->get('test.get');

$this->assertInstanceOf(OperationInterface::class, $operation);
$this->assertEquals('A long **Test** description', $operation->getDescription());
Expand All @@ -59,7 +59,7 @@ public function testParseTypeHint()
{
$annotation = new AttributeParser($this->schemaManager);
$specification = $annotation->parse(BarController::class);
$operation = $specification->getOperations()->get('PSX.Api.Tests.Parser.Attribute.BarController.myMethod');
$operation = $specification->getOperations()->get('tests.parser.attribute.bar_controller.myMethod');

$this->assertInstanceOf(OperationInterface::class, $operation);
$this->assertEquals('path', $operation->getArguments()->get('id')->getIn());
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/ParserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testParseSimple()
{
$specification = $this->getSpecification();
$definitions = $specification->getDefinitions();
$operation = $specification->getOperations()->get('PSX.Api.Tests.Parser.Attribute.TestController.doGet');
$operation = $specification->getOperations()->get('test.get');

$this->assertInstanceOf(OperationInterface::class, $operation);
$this->assertEquals('GET', $operation->getMethod());
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/openapi/simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}],
"get": {
"summary": "A long **Test** description",
"operationId": "PSX.Api.Tests.Parser.Attribute.TestController.doGet",
"operationId": "test.get",
"tags": ["foo"],
"parameters": [{
"name": "foo",
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/typeapi/simple.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"operations": {
"PSX.Api.Tests.Parser.Attribute.TestController.doGet": {
"test.get": {
"description": "A long **Test** description",
"method": "GET",
"path": "/foo/:fooId",
Expand Down

0 comments on commit 443d39e

Please sign in to comment.