Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public function build(\Reflector $reflector, Context $context): array
}
}
}

if ($reflector instanceof \ReflectionClass) {
foreach ($reflector->getReflectionConstants() as $rc) {
foreach ($rc->getAttributes(Property::class) as $attribute) {
$instance = $attribute->newInstance();
if (Generator::isDefault($instance->property)) {
$instance->property = $rc->getName();
}
if (Generator::isDefault($instance->type)) {
$instance->const = $rc->getValue();
}
$annotations[] = $instance;
}
}
}
} finally {
Generator::$context = null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ class Schema extends AbstractAnnotation

/**
* http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.24.
*
* @var string
*/
public $const = Generator::UNDEFINED;

Expand All @@ -362,6 +364,7 @@ class Schema extends AbstractAnnotation
'allOf' => '[' . Schema::class . ']',
'oneOf' => '[' . Schema::class . ']',
'anyOf' => '[' . Schema::class . ']',
'const' => 'string',
];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Attributes/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use OpenApi\Generator;

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER)]
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT)]
class Property extends \OpenApi\Annotations\Property
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/Attributes/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function __construct(
?int $minItems = null,
?bool $uniqueItems = null,
?string $pattern = null,
?string $const = null,
?array $enum = null,
?Discriminator $discriminator = null,
?bool $readOnly = null,
Expand Down Expand Up @@ -78,6 +79,7 @@ public function __construct(
'minItems' => $minItems ?? Generator::UNDEFINED,
'uniqueItems' => $uniqueItems ?? Generator::UNDEFINED,
'pattern' => $pattern ?? Generator::UNDEFINED,
'const' => $const ?? Generator::UNDEFINED,
'enum' => $enum ?? Generator::UNDEFINED,
'readOnly' => $readOnly ?? Generator::UNDEFINED,
'writeOnly' => $writeOnly ?? Generator::UNDEFINED,
Expand Down
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
36 changes: 36 additions & 0 deletions src/Processors/BackportConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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->enum = [$schema->const];
$schema->const = Generator::UNDEFINED;
}
}
}
}