44
55namespace Tobion \OpenApiSymfonyRouting ;
66
7+ use OpenApi \Analysis ;
8+ use OpenApi \Annotations \OpenApi ;
79use OpenApi \Annotations \Operation ;
10+ use OpenApi \Generator ;
11+ use OpenApi \Processors \DocBlockDescriptions ;
12+ use OpenApi \Processors \OperationId ;
813use Symfony \Bundle \FrameworkBundle \Routing \RouteLoaderInterface ;
914use Symfony \Component \Finder \Finder ;
1015use 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