Skip to content

Commit 230399d

Browse files
committed
add processor to backport const keyword for open api < 3.1
1 parent 874616f commit 230399d

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Generator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ public function getProcessors(): array
191191
new Processors\MergeXmlContent(),
192192
new Processors\OperationId(),
193193
new Processors\CleanUnmerged(),
194+
new Processors\BackportConstants(),
194195
];
195196
}
196197

src/Processors/BackportConstants.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Processors;
8+
9+
use OpenApi\Analysis;
10+
use OpenApi\Annotations\OpenApi;
11+
use OpenApi\Annotations\Property as AnnotationSchema;
12+
use OpenApi\Attributes\Property as AttributeSchema;
13+
use OpenApi\Generator;
14+
15+
/**
16+
* Transform const keywords to enum in OpenApi versions < 3.1.0.
17+
*/
18+
class BackportConstants
19+
{
20+
public function __invoke(Analysis $analysis)
21+
{
22+
/** @var AnnotationSchema[] $schemas */
23+
$schemas = $analysis->getAnnotationsOfType([AnnotationSchema::class, AttributeSchema::class], true);
24+
25+
foreach ($schemas as $schema) {
26+
if (Generator::isDefault($schema->const)) {
27+
continue;
28+
}
29+
30+
if (version_compare($analysis->context->version, OpenApi::VERSION_3_1_0, '<')) {
31+
$schema->type = 'enum';
32+
$schema->enum = [$schema->const];
33+
$schema->const = Generator::UNDEFINED;
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)