Reproduce and fix duplicate operation id errors (#1972)

* Replicate and fix duplicate operation id errors

* Fix CS
This commit is contained in:
Guilhem Niot 2022-03-28 14:36:03 +02:00 committed by GitHub
parent 9cd417efb4
commit 8111ce645b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 44 deletions

View File

@ -15,7 +15,6 @@ use Nelmio\ApiDocBundle\Describer\DescriberInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface; use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Model\ModelRegistry; use Nelmio\ApiDocBundle\Model\ModelRegistry;
use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface; use Nelmio\ApiDocBundle\ModelDescriber\ModelDescriberInterface;
use Nelmio\ApiDocBundle\OpenApiPhp\DefaultOperationId;
use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister; use Nelmio\ApiDocBundle\OpenApiPhp\ModelRegister;
use Nelmio\ApiDocBundle\OpenApiPhp\Util; use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Analysis; use OpenApi\Analysis;
@ -84,6 +83,14 @@ final class ApiDocGenerator
} }
} }
$generator = new 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 https://github.com/zircote/swagger-php/issues/1153
$generator->setProcessors(array_filter($generator->getProcessors(), function ($processor) {
return !$processor instanceof \OpenApi\Processors\OperationId;
}));
$this->openApi = new OpenApi([]); $this->openApi = new OpenApi([]);
$modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames); $modelRegistry = new ModelRegistry($this->modelDescribers, $this->openApi, $this->alternativeNames);
if (null !== $this->logger) { if (null !== $this->logger) {
@ -97,7 +104,12 @@ final class ApiDocGenerator
$describer->describe($this->openApi); $describer->describe($this->openApi);
} }
$context = Util::createContext(); $context = Util::createContext(
// BC for for zircote/swagger-php < 4.2
method_exists($generator, 'getVersion')
? ['version' => $generator->getVersion()]
: []
);
$analysis = new Analysis([], $context); $analysis = new Analysis([], $context);
$analysis->addAnnotation($this->openApi, $context); $analysis->addAnnotation($this->openApi, $context);
@ -108,10 +120,7 @@ final class ApiDocGenerator
// Calculate the associated schemas // Calculate the associated schemas
$modelRegistry->registerSchemas(); $modelRegistry->registerSchemas();
$defaultOperationIdProcessor = new DefaultOperationId(); $analysis->process($generator->getProcessors());
$defaultOperationIdProcessor($analysis);
$analysis->process((new Generator())->getProcessors());
$analysis->validate(); $analysis->validate();
if (isset($item)) { if (isset($item)) {

View File

@ -1,36 +0,0 @@
<?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\OpenApiPhp;
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
/**
* Disable the OperationId processor from zircote/swagger-php as it breaks our documentation by setting non-unique operation ids.
* See https://github.com/zircote/swagger-php/pull/483#issuecomment-360739260 for the solution used here.
*
* @internal
*/
final class DefaultOperationId
{
public function __invoke(Analysis $analysis)
{
$allOperations = $analysis->getAnnotationsOfType(OA\Operation::class);
foreach ($allOperations as $operation) {
if (Generator::UNDEFINED === $operation->operationId) {
$operation->operationId = null;
}
}
}
}

View File

@ -316,8 +316,6 @@ final class Util
*/ */
public static function createContext(array $properties = [], Context $parent = null): Context public static function createContext(array $properties = [], Context $parent = null): Context
{ {
$properties['comment'] = ''; // TODO: remove this when https://github.com/zircote/swagger-php/commit/708a25208797ca05ebeae572bbccad8b13de14d8 is released
return new Context($properties, $parent); return new Context($properties, $parent);
} }

View File

@ -186,6 +186,20 @@ class TestKernel extends Kernel
'info' => [ 'info' => [
'title' => 'My Default App', 'title' => 'My Default App',
], ],
'paths' => [
// Ensures we can define routes in Yaml without defining OperationIds
// See https://github.com/zircote/swagger-php/issues/1153
'/api/test-from-yaml' => ['get' => [
'responses' => [
200 => ['description' => 'success'],
],
]],
'/api/test-from-yaml2' => ['get' => [
'responses' => [
200 => ['description' => 'success'],
],
]],
],
'components' => [ 'components' => [
'schemas' => [ 'schemas' => [
'Test' => [ 'Test' => [