From 132b4c1716f9d4ae2811645394e4b1d6619c5949 Mon Sep 17 00:00:00 2001 From: Wouter Bohlken Date: Mon, 4 Dec 2023 16:20:00 +0100 Subject: [PATCH] Add config option to convert operationIds into camel case --- config/swagger-generator.php | 6 ++++++ src/RoutesParser.php | 8 ++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config/swagger-generator.php b/config/swagger-generator.php index 9c40b23..ededf28 100644 --- a/config/swagger-generator.php +++ b/config/swagger-generator.php @@ -77,6 +77,12 @@ 'ignoredAnnotationNames' => ['mixin'], /* |--------------------------------------------------------- + | Use camel case for operation IDs instead of dots + |--------------------------------------------------------- + */ + 'camelCaseOperationIds' => false, + /* + |--------------------------------------------------------- | List of classes to additionally generate definitions |--------------------------------------------------------- | diff --git a/src/RoutesParser.php b/src/RoutesParser.php index 9c51336..fd52ed9 100644 --- a/src/RoutesParser.php +++ b/src/RoutesParser.php @@ -88,6 +88,7 @@ public function parse(): array $matches = config('swagger-generator.routes.matches', []); $matchesNot = config('swagger-generator.routes.notMatches', []); $documentedMethods = config('swagger-generator.routes.methods', ['GET']); + $camelCaseOperationIds = config('swagger-generator.camelCaseOperationIds', false); $this->routeNum = 1; foreach ($this->routes as $route) { if (! $this->checkRoute($route, $matches, $matchesNot, $only, $except)) { @@ -98,7 +99,7 @@ public function parse(): array if ($ref instanceof \ReflectionFunction) { $this->trigger(static::EVENT_PROBLEM_FOUND, static::PROBLEM_ROUTE_CLOSURE, $route); } - $this->parseRoute($paths, $route, $documentedMethods, $this->routeNum); + $this->parseRoute($paths, $route, $documentedMethods, $this->routeNum, $camelCaseOperationIds); ++$this->routeNum; $this->trigger(static::EVENT_ROUTE_PROCESSED, $route); } @@ -116,7 +117,7 @@ public function parse(): array * @param int $num * @return array */ - protected function parseRoute(array &$data, Route $route, array $documentedMethods, int $num = 1): array + protected function parseRoute(array &$data, Route $route, array $documentedMethods, int $num = 1, bool $camelCaseOperationIds = false): array { $routeWoBody = ! empty(array_intersect(['GET', 'HEAD'], $route->methods)); $routeData = [ @@ -151,6 +152,9 @@ protected function parseRoute(array &$data, Route $route, array $documentedMetho $tag = ! empty($routeData['tags']) ? reset($routeData['tags']) : 'default'; $routeData['operationId'] = $this->getRouteId($route, $tag); + if ($camelCaseOperationIds) { + $routeData['operationId'] = Str::camel(str_replace('.', '_', $routeData['operationId'])); + } $path = $this->normalizeUri($route->uri(), true); $methods = array_intersect($route->methods, $documentedMethods); $dataRows = [