Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow zircote/swagger-php v5 #2420

Merged
merged 3 commits into from
Jan 17, 2025
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
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"
"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 @@ -45,7 +45,7 @@ public function process(ContainerBuilder $container): void
}
}

$definition->addMethodCall('addProcessor', [$reference, $before]);
$definition->addMethodCall('addNelmioProcessor', [$reference, $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
31 changes: 31 additions & 0 deletions src/OpenApiGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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.
*
* @internal
*/
final 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);
}
}
}
Loading