Skip to content

Commit

Permalink
Move AugmentTags after CleanUnusedComponents (zircote#1630)
Browse files Browse the repository at this point in the history
Order is import here as cleanup of tags can only happen after `CleanUnusedComponents` has run.
  • Loading branch information
DerManoMann authored Jul 16, 2024
1 parent 2a6fa1d commit 0840dad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ public function getProcessorPipeline(): Pipeline
new Processors\MergeJsonContent(),
new Processors\MergeXmlContent(),
new Processors\OperationId(),
new Processors\AugmentTags(),
new Processors\CleanUnmerged(),
new Processors\PathFilter(),
new Processors\CleanUnusedComponents(),
new Processors\AugmentTags(),
]);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Fixtures/SurplusTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures;

use OpenApi\Attributes as OAT;

#[OAT\OpenApi(
info: new OAT\Info(
title: 'test',
description: 'test',
version: '2.0.0'
),
)]
class SurplusTag
{
#[OAT\Get(path: '/world/', tags: ['tag world'], responses: [new OAT\Response(response: '200', description: 'success')])]
#[OAT\Get(path: '/hello/', tags: ['tag hello'], responses: [new OAT\Response(response: '200', description: 'success')])]
public function hello(): void
{
}
}
3 changes: 2 additions & 1 deletion tests/OpenApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ public static function processors(array $strip = []): array
return $processors;
}

public function analysisFromFixtures(array $files, array $processors = [], ?AnalyserInterface $analyzer = null): Analysis
public function analysisFromFixtures(array $files, array $processors = [], ?AnalyserInterface $analyzer = null, array $config = []): Analysis
{
$analysis = new Analysis([], $this->getContext());

(new Generator($this->getTrackingLogger()))
->setConfig($config)
->setAnalyser($analyzer ?: $this->getAnalyzer())
->setProcessorPipeline(new Pipeline($processors))
->generate($this->fixtures($files), $analysis, false);
Expand Down
28 changes: 28 additions & 0 deletions tests/Processors/AugmentTagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Processors;

use OpenApi\Tests\OpenApiTestCase;

class AugmentTagsTest extends OpenApiTestCase
{
/**
* @requires PHP 8.1
*/
public function testAugmentTags(): void
{
$this->skipLegacy();

$config = [
'pathFilter' => ['paths' => ['#^/hello/#']],
'cleanUnusedComponents' => ['enabled' => true],
];
$analysis = $this->analysisFromFixtures(['SurplusTag.php'], static::processors(), null, $config);

$this->assertCount(1, $analysis->openapi->tags);
}
}
15 changes: 7 additions & 8 deletions tests/Processors/CleanUnusedComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@
namespace OpenApi\Tests\Processors;

use OpenApi\Generator;
use OpenApi\Processors\CleanUnusedComponents;
use OpenApi\Tests\OpenApiTestCase;

class CleanUnusedComponentsTest extends OpenApiTestCase
{
public static function countCases(): iterable
{
$defaultProcessors = static::processors([CleanUnusedComponents::class]);
$configEnable = ['cleanUnusedComponents' => ['enabled' => true]];

return [
'var-default' => [$defaultProcessors, 'UsingVar.php', 2, 5],
'var-clean' => [array_merge($defaultProcessors, [new CleanUnusedComponents(true)]), 'UsingVar.php', 0, 2],
'unreferenced-default' => [$defaultProcessors, 'Unreferenced.php', 2, 11],
'unreferenced-clean' => [array_merge($defaultProcessors, [new CleanUnusedComponents(true)]), 'Unreferenced.php', 0, 5],
'var-default' => [[], 'UsingVar.php', 2, 5],
'var-clean' => [$configEnable, 'UsingVar.php', 0, 2],
'unreferenced-default' => [[], 'Unreferenced.php', 2, 11],
'unreferenced-clean' => [$configEnable, 'Unreferenced.php', 0, 5],
];
}

/**
* @dataProvider countCases
*/
public function testCounts(array $processors, string $fixture, int $expectedSchemaCount, int $expectedAnnotationCount): void
public function testCounts(array $config, string $fixture, int $expectedSchemaCount, int $expectedAnnotationCount): void
{
$analysis = $this->analysisFromFixtures([$fixture], $processors);
$analysis = $this->analysisFromFixtures([$fixture], static::processors(), null, $config);

if ($expectedSchemaCount === 0) {
$this->assertTrue(Generator::isDefault($analysis->openapi->components->schemas));
Expand Down

0 comments on commit 0840dad

Please sign in to comment.