Skip to content

Commit

Permalink
add processor to backport const keyword for open api < 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaguerre committed Mar 2, 2022
1 parent 874616f commit 107d9b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ public function getProcessors(): array
new Processors\MergeXmlContent(),
new Processors\OperationId(),
new Processors\CleanUnmerged(),
new Processors\BackportConstants(),
];
}

Expand Down
37 changes: 37 additions & 0 deletions src/Processors/BackportConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Processors;

use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Property as AnnotationSchema;
use OpenApi\Attributes\Property as AttributeSchema;
use OpenApi\Generator;

/**
* Transform const keywords to enum in OpenApi versions < 3.1.0
*/
class BackportConstants
{
public function __invoke(Analysis $analysis)
{
/** @var AnnotationSchema[] $schemas */
$schemas = $analysis->getAnnotationsOfType([AnnotationSchema::class, AttributeSchema::class], true);

foreach ($schemas as $schema) {
if (Generator::isDefault($schema->const)) {
continue;
}

if (version_compare($analysis->context->version, OpenApi::VERSION_3_1_0, '<')) {
$schema->type = 'enum';
$schema->enum = [$schema->const];
$schema->const = Generator::UNDEFINED;
}
}
}
}

0 comments on commit 107d9b4

Please sign in to comment.