diff --git a/config/l5-swagger.php b/config/l5-swagger.php index b96ed4c..ff4c8a4 100644 --- a/config/l5-swagger.php +++ b/config/l5-swagger.php @@ -144,13 +144,19 @@ 'analysis' => null, /** - * Custom query path processors classes. + * Custom processors. + * + * Each entry can be: + * - A class name or instance (inserted after BuildPaths by default) + * - An array with 'class' and 'after' keys for precise positioning: + * ['class' => MyProcessor::class, 'after' => SomeProcessor::class] * * @link https://github.com/zircote/swagger-php/tree/master/Examples/processors/schema-query-parameter * @see \OpenApi\scan */ 'processors' => [ - // new \App\SwaggerProcessors\SchemaQueryParameter(), + // \App\SwaggerProcessors\SchemaQueryParameter::class, + // ['class' => \App\SwaggerProcessors\Custom::class, 'after' => \OpenApi\Processors\AugmentSchemas::class], ], /** diff --git a/src/Generator.php b/src/Generator.php index 3cf969e..9a14a20 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -223,23 +223,34 @@ protected function createOpenApiGenerator(): OpenApiGenerator */ protected function setProcessors(OpenApiGenerator $generator): void { - $processorClasses = Arr::get($this->scanOptions, self::SCAN_OPTION_PROCESSORS, []); - $newPipeLine = []; + $processorConfigs = Arr::get($this->scanOptions, self::SCAN_OPTION_PROCESSORS, []); + + if (empty($processorConfigs)) { + return; + } + $normalizedConfigs = []; + foreach ($processorConfigs as $config) { + $normalizedConfigs[] = is_array($config) + ? $config + : ['class' => $config, 'after' => \OpenApi\Processors\BuildPaths::class]; + } + + $newPipeLine = []; $generator->getProcessorPipeline()->walk( - function (callable $pipe) use ($processorClasses, &$newPipeLine) { + function (callable $pipe) use ($normalizedConfigs, &$newPipeLine) { $newPipeLine[] = $pipe; - if ($pipe instanceof \OpenApi\Processors\BuildPaths) { - foreach ($processorClasses as $customProcessor) { - $newPipeLine[] = new $customProcessor(); + foreach ($normalizedConfigs as $entry) { + $after = $entry['after']; + if ($pipe instanceof $after) { + $processor = $entry['class']; + $newPipeLine[] = is_string($processor) ? new $processor() : $processor; } } } ); - if (! empty($newPipeLine)) { - $generator->setProcessorPipeline(new Pipeline($newPipeLine)); - } + $generator->setProcessorPipeline(new Pipeline($newPipeLine)); } /** diff --git a/tests/Unit/GeneratorTest.php b/tests/Unit/GeneratorTest.php index 1b0be0e..e29acde 100644 --- a/tests/Unit/GeneratorTest.php +++ b/tests/Unit/GeneratorTest.php @@ -14,6 +14,7 @@ use OpenApi\Generator as OpenApiGenerator; use OpenApi\OpenApiException; use OpenApi\Processors\AugmentParameters; +use OpenApi\Processors\AugmentSchemas; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\TestDox; use Symfony\Component\Yaml\Parser; @@ -228,6 +229,38 @@ public function testCanGenerateWithScanOptions(): void ->assertStatus(200); } + /** + * @throws L5SwaggerException + */ + public function testCanGenerateWithProcessorPositioning(): void + { + $cfg = config('l5-swagger.documentations.default'); + + $cfg['scanOptions'] = [ + 'processors' => [ + new AugmentParameters(), + ['class' => AugmentSchemas::class, 'after' => AugmentParameters::class], + ], + 'default_processors_configuration' => ['operationId' => ['hash' => false]], + ]; + + config(['l5-swagger' => [ + 'default' => 'default', + 'documentations' => ['default' => $cfg], + 'defaults' => config('l5-swagger.defaults'), + ]]); + + $this->setAnnotationsPath(); + + $this->generator->generateDocs(); + + $this->assertFileExists($this->jsonDocsFile()); + + $this->get(route('l5-swagger.default.docs')) + ->assertSee('L5 Swagger') + ->assertStatus(200); + } + /** * @throws L5SwaggerException */