Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions config/l5-swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],

/**
Expand Down
29 changes: 20 additions & 9 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Loading