From 443d39e2a8733428b753bbec4e5305f78309510b Mon Sep 17 00:00:00 2001 From: Christoph Kappestein Date: Thu, 22 Aug 2024 17:46:49 +0200 Subject: [PATCH] improve build operation id --- src/Parser/Attribute.php | 18 +++++++++++++++++- tests/Console/resource/spec_openapi.json | 2 +- tests/Parser/Attribute/BarController.php | 1 + tests/Parser/Attribute/FooController.php | 2 ++ tests/Parser/Attribute/TestController.php | 6 ++++-- tests/Parser/AttributeTest.php | 4 ++-- tests/Parser/ParserTestCase.php | 2 +- tests/Parser/openapi/simple.json | 2 +- tests/Parser/typeapi/simple.json | 2 +- 9 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Parser/Attribute.php b/src/Parser/Attribute.php index 7645d271..7b7472e6 100644 --- a/src/Parser/Attribute.php +++ b/src/Parser/Attribute.php @@ -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)); } } diff --git a/tests/Console/resource/spec_openapi.json b/tests/Console/resource/spec_openapi.json index b6ee49a5..55827634 100644 --- a/tests/Console/resource/spec_openapi.json +++ b/tests/Console/resource/spec_openapi.json @@ -16,7 +16,7 @@ "foo" ], "description": "A long **Test** description", - "operationId": "PSX.Api.Tests.Parser.Attribute.TestController.doGet", + "operationId": "test.get", "parameters": [ { "name": "foo", diff --git a/tests/Parser/Attribute/BarController.php b/tests/Parser/Attribute/BarController.php index 2d7ebe60..89de165e 100644 --- a/tests/Parser/Attribute/BarController.php +++ b/tests/Parser/Attribute/BarController.php @@ -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; diff --git a/tests/Parser/Attribute/FooController.php b/tests/Parser/Attribute/FooController.php index 288d1d07..8aebe00e 100644 --- a/tests/Parser/Attribute/FooController.php +++ b/tests/Parser/Attribute/FooController.php @@ -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; @@ -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() { } diff --git a/tests/Parser/Attribute/TestController.php b/tests/Parser/Attribute/TestController.php index 7390a1a6..140a4d99 100644 --- a/tests/Parser/Attribute/TestController.php +++ b/tests/Parser/Attribute/TestController.php @@ -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; @@ -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) { } } diff --git a/tests/Parser/AttributeTest.php b/tests/Parser/AttributeTest.php index dbf151f2..4ebddac6 100644 --- a/tests/Parser/AttributeTest.php +++ b/tests/Parser/AttributeTest.php @@ -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()); @@ -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()); diff --git a/tests/Parser/ParserTestCase.php b/tests/Parser/ParserTestCase.php index 7636e28b..e4a278d3 100644 --- a/tests/Parser/ParserTestCase.php +++ b/tests/Parser/ParserTestCase.php @@ -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()); diff --git a/tests/Parser/openapi/simple.json b/tests/Parser/openapi/simple.json index 79bce269..a1feba6d 100644 --- a/tests/Parser/openapi/simple.json +++ b/tests/Parser/openapi/simple.json @@ -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", diff --git a/tests/Parser/typeapi/simple.json b/tests/Parser/typeapi/simple.json index 13660ba2..ba2cb924 100644 --- a/tests/Parser/typeapi/simple.json +++ b/tests/Parser/typeapi/simple.json @@ -1,6 +1,6 @@ { "operations": { - "PSX.Api.Tests.Parser.Attribute.TestController.doGet": { + "test.get": { "description": "A long **Test** description", "method": "GET", "path": "/foo/:fooId",