Skip to content

Commit

Permalink
Upgrade to use zircote/swagger-php processor pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver committed Jan 15, 2025
1 parent ea6550d commit 701ab46
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"symfony/options-resolver": "^5.4 || ^6.4 || ^7.1",
"symfony/property-info": "^5.4.10 || ^6.4 || ^7.1",
"symfony/routing": "^5.4 || ^6.4 || ^7.1",
"zircote/swagger-php": "^4.6.1 || ^5.0"
"zircote/swagger-php": "^4.11.1 || ^5.0"
},
"require-dev": {
"api-platform/core": "^2.7.0 || ^3",
Expand Down
31 changes: 5 additions & 26 deletions src/ApiDocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function generate(): OpenApi
$this->generator->setVersion($this->openApiVersion);
}

$this->generator->setProcessors($this->getProcessors($this->generator));
// Remove OperationId processor as we use a lot of generated annotations which do not have enough information in their context
// to generate these ids properly.
// @see \Nelmio\ApiDocBundle\OpenApiPhp\Util::createContext
$this->generator->getProcessorPipeline()->remove(\OpenApi\Processors\OperationId::class);

$context = Util::createContext(['version' => $this->generator->getVersion()]);

Expand All @@ -130,7 +133,7 @@ public function generate(): OpenApi
// Calculate the associated schemas
$modelRegistry->registerSchemas();

$analysis->process($this->generator->getProcessors());
$this->generator->getProcessorPipeline()->process($analysis);
$analysis->validate();

if (isset($item)) {
Expand All @@ -139,28 +142,4 @@ public function generate(): OpenApi

return $this->openApi;
}

/**
* Get an array of processors that will be used to process the OpenApi object.
*
* @param Generator $generator The generator instance to get the standard processors from
*
* @return array<callable> The array of processors
*/
private function getProcessors(Generator $generator): array
{
// Get the standard processors from the generator.
$processors = $generator->getProcessors();

// Remove OperationId processor as we use a lot of generated annotations which do not have enough information in their context
// to generate these ids properly.
// @see \Nelmio\ApiDocBundle\OpenApiPhp\Util::createContext
foreach ($processors as $key => $processor) {
if ($processor instanceof \OpenApi\Processors\OperationId) {
unset($processors[$key]);
}
}

return $processors;
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/CustomProcessorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function process(ContainerBuilder $container): void
}
}

$definition->addMethodCall('addProcessor', [new Reference($id), $before]);
$definition->addMethodCall('addNelmioProcessor', [new Reference($id), $before]);
}
}
}
3 changes: 2 additions & 1 deletion src/DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Nelmio\ApiDocBundle\ModelDescriber\BazingaHateoasModelDescriber;
use Nelmio\ApiDocBundle\ModelDescriber\JMSModelDescriber;
use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface;
use Nelmio\ApiDocBundle\OpenApiGenerator;
use Nelmio\ApiDocBundle\Processor\MapQueryStringProcessor;
use Nelmio\ApiDocBundle\Processor\MapRequestPayloadProcessor;
use Nelmio\ApiDocBundle\RouteDescriber\RouteArgumentDescriber;
Expand Down Expand Up @@ -75,7 +76,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('nelmio_api_doc.use_validation_groups', $config['use_validation_groups']);

// Register the OpenAPI Generator as a service.
$container->register('nelmio_api_doc.open_api.generator', Generator::class)
$container->register('nelmio_api_doc.open_api.generator', OpenApiGenerator::class)
->setPublic(false);

$cachePool = $config['cache']['pool'] ?? null;
Expand Down
29 changes: 29 additions & 0 deletions src/OpenApiGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\ApiDocBundle;

use OpenApi\Generator;

/**
* Extension of OpenApi\Generator to be able to inject processors with dependency injection.
*/
class OpenApiGenerator extends Generator
{
public function addNelmioProcessor(callable $processor, ?string $before = null): void
{
if (null === $before) {
$this->getProcessorPipeline()->add($processor);
} else {
$this->getProcessorPipeline()->insert($processor, $before);

Check warning on line 26 in src/OpenApiGenerator.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApiGenerator.php#L26

Added line #L26 was not covered by tests
}
}
}

0 comments on commit 701ab46

Please sign in to comment.