Skip to content

Commit 48a08be

Browse files
authored
Merge pull request #10 from Tobion/fix-default-route-name-swagger-3.2.2
Fix default route name with swagger-php >=3.2.2 which hashes the operationId
2 parents fb790c9 + 96ff5af commit 48a08be

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

.github/workflows/ci.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ on:
1010
- cron: '0 11 26 * *'
1111

1212
jobs:
13-
build-lowest-version:
14-
name: Build lowest version
13+
build-lowest-dependencies:
14+
name: With lowest dependencies
1515
runs-on: ubuntu-latest
1616

1717
steps:
@@ -53,3 +53,27 @@ jobs:
5353

5454
- name: Run tests
5555
run: vendor/bin/simple-phpunit
56+
57+
build-dev-dependencies:
58+
name: With dev dependencies & PHP
59+
runs-on: ubuntu-latest
60+
61+
steps:
62+
- name: Set up PHP
63+
uses: shivammathur/setup-php@v2
64+
with:
65+
php-version: 8.1
66+
coverage: 'none'
67+
68+
- name: Checkout code
69+
uses: actions/checkout@v2
70+
71+
- name: Allow dev dependencies
72+
run: composer config minimum-stability dev
73+
74+
- name: Install dependencies
75+
run: composer update --no-interaction --no-progress --prefer-dist
76+
77+
- name: Run tests
78+
continue-on-error: true
79+
run: vendor/bin/simple-phpunit

src/OpenApiRouteLoader.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
namespace Tobion\OpenApiSymfonyRouting;
66

7+
use OpenApi\Analysis;
8+
use OpenApi\Annotations\OpenApi;
79
use OpenApi\Annotations\Operation;
10+
use OpenApi\Generator;
11+
use OpenApi\Processors\DocBlockDescriptions;
12+
use OpenApi\Processors\OperationId;
813
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
914
use Symfony\Component\Finder\Finder;
1015
use Symfony\Component\Routing\Route;
@@ -22,9 +27,18 @@ class OpenApiRouteLoader implements RouteLoaderInterface
2227
*/
2328
private $routeNames = [];
2429

30+
/**
31+
* @var string
32+
*/
33+
private static $openApiUndefined;
34+
2535
public function __construct(Finder $finder)
2636
{
2737
$this->finder = $finder;
38+
39+
if (!isset(self::$openApiUndefined)) {
40+
self::$openApiUndefined = \class_exists(Generator::class) ? Generator::UNDEFINED : \OpenApi\UNDEFINED;
41+
}
2842
}
2943

3044
public static function fromDirectories(string $dir, string ...$moreDirs): self
@@ -44,7 +58,7 @@ public static function fromSrcDirectory(): self
4458

4559
public function __invoke(): RouteCollection
4660
{
47-
$openApi = \OpenApi\scan($this->finder);
61+
$openApi = $this->createOpenApi();
4862
$routeCollection = new RouteCollection();
4963

5064
$globalFormatSuffixConfig = FormatSuffixConfig::fromAnnotation($openApi);
@@ -66,12 +80,26 @@ public function __invoke(): RouteCollection
6680
return $routeCollection;
6781
}
6882

83+
private function createOpenApi(): OpenApi
84+
{
85+
if (!\class_exists(Generator::class)) {
86+
return \OpenApi\scan($this->finder);
87+
}
88+
89+
$processors = array_filter(Analysis::processors(), static function ($processor): bool {
90+
// remove OperationId processor which would hash the controller starting in 3.2.2 breaking the default route name logic
91+
return !$processor instanceof OperationId && !$processor instanceof DocBlockDescriptions;
92+
});
93+
94+
return (new Generator())->setProcessors($processors)->generate($this->finder);
95+
}
96+
6997
/**
7098
* @param Operation|string $operation
7199
*/
72100
private function addRouteFromOpenApiOperation(RouteCollection $routeCollection, $operation, FormatSuffixConfig $parentFormatSuffixConfig): void
73101
{
74-
if (\OpenApi\UNDEFINED === $operation || !$operation instanceof Operation) {
102+
if (self::$openApiUndefined === $operation || !$operation instanceof Operation) {
75103
return;
76104
}
77105

@@ -98,9 +126,9 @@ private function createRoute(Operation $operation, string $controller, FormatSuf
98126
$route->setRequirement('_format', $formatSuffixConfig->pattern);
99127
}
100128
}
101-
if (\OpenApi\UNDEFINED !== $operation->parameters) {
129+
if (self::$openApiUndefined !== $operation->parameters) {
102130
foreach ($operation->parameters as $parameter) {
103-
if ('path' === $parameter->in && \OpenApi\UNDEFINED !== $parameter->schema && \OpenApi\UNDEFINED !== $parameter->schema->pattern) {
131+
if ('path' === $parameter->in && self::$openApiUndefined !== $parameter->schema && self::$openApiUndefined !== $parameter->schema->pattern) {
104132
$route->setRequirement($parameter->name, $parameter->schema->pattern);
105133
}
106134
}
@@ -121,7 +149,7 @@ private function getRouteName(Operation $operation, string $controller): string
121149
// swagger-php v3 adds the controller as operationId automatically, see \OpenApi\Processors\OperationId.
122150
// This must be ignored as it is not viable with multiple annotations on the same controller.
123151

124-
return \OpenApi\UNDEFINED === $operation->operationId || $controller === $operation->operationId ? $this->getDefaultRouteName($controller) : $operation->operationId;
152+
return self::$openApiUndefined === $operation->operationId || $controller === $operation->operationId ? $this->getDefaultRouteName($controller) : $operation->operationId;
125153
}
126154

127155
private function getRoutePriority(Operation $operation): int

0 commit comments

Comments
 (0)