4
4
5
5
namespace Tobion \OpenApiSymfonyRouting ;
6
6
7
+ use OpenApi \Analysis ;
8
+ use OpenApi \Annotations \OpenApi ;
7
9
use OpenApi \Annotations \Operation ;
10
+ use OpenApi \Generator ;
11
+ use OpenApi \Processors \DocBlockDescriptions ;
12
+ use OpenApi \Processors \OperationId ;
8
13
use Symfony \Bundle \FrameworkBundle \Routing \RouteLoaderInterface ;
9
14
use Symfony \Component \Finder \Finder ;
10
15
use Symfony \Component \Routing \Route ;
@@ -22,9 +27,18 @@ class OpenApiRouteLoader implements RouteLoaderInterface
22
27
*/
23
28
private $ routeNames = [];
24
29
30
+ /**
31
+ * @var string
32
+ */
33
+ private static $ openApiUndefined ;
34
+
25
35
public function __construct (Finder $ finder )
26
36
{
27
37
$ this ->finder = $ finder ;
38
+
39
+ if (!isset (self ::$ openApiUndefined )) {
40
+ self ::$ openApiUndefined = \class_exists (Generator::class) ? Generator::UNDEFINED : \OpenApi \UNDEFINED ;
41
+ }
28
42
}
29
43
30
44
public static function fromDirectories (string $ dir , string ...$ moreDirs ): self
@@ -44,7 +58,7 @@ public static function fromSrcDirectory(): self
44
58
45
59
public function __invoke (): RouteCollection
46
60
{
47
- $ openApi = \ OpenApi \scan ( $ this ->finder );
61
+ $ openApi = $ this ->createOpenApi ( );
48
62
$ routeCollection = new RouteCollection ();
49
63
50
64
$ globalFormatSuffixConfig = FormatSuffixConfig::fromAnnotation ($ openApi );
@@ -66,12 +80,26 @@ public function __invoke(): RouteCollection
66
80
return $ routeCollection ;
67
81
}
68
82
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
+
69
97
/**
70
98
* @param Operation|string $operation
71
99
*/
72
100
private function addRouteFromOpenApiOperation (RouteCollection $ routeCollection , $ operation , FormatSuffixConfig $ parentFormatSuffixConfig ): void
73
101
{
74
- if (\ OpenApi \ UNDEFINED === $ operation || !$ operation instanceof Operation) {
102
+ if (self :: $ openApiUndefined === $ operation || !$ operation instanceof Operation) {
75
103
return ;
76
104
}
77
105
@@ -98,9 +126,9 @@ private function createRoute(Operation $operation, string $controller, FormatSuf
98
126
$ route ->setRequirement ('_format ' , $ formatSuffixConfig ->pattern );
99
127
}
100
128
}
101
- if (\ OpenApi \ UNDEFINED !== $ operation ->parameters ) {
129
+ if (self :: $ openApiUndefined !== $ operation ->parameters ) {
102
130
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 ) {
104
132
$ route ->setRequirement ($ parameter ->name , $ parameter ->schema ->pattern );
105
133
}
106
134
}
@@ -121,7 +149,7 @@ private function getRouteName(Operation $operation, string $controller): string
121
149
// swagger-php v3 adds the controller as operationId automatically, see \OpenApi\Processors\OperationId.
122
150
// This must be ignored as it is not viable with multiple annotations on the same controller.
123
151
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 ;
125
153
}
126
154
127
155
private function getRoutePriority (Operation $ operation ): int
0 commit comments