-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add processor to backport const keyword for open api < 3.1
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |