Allow original backing value in Enum (#1239)
This commit is contained in:
parent
d6d9a713fa
commit
4087d4fe5b
|
@ -34,11 +34,21 @@ class ExpandEnums
|
|||
$re = new \ReflectionEnum($schema->_context->fullyQualifiedName($source));
|
||||
$schema->schema = !Generator::isDefault($schema->schema) ? $schema->schema : $re->getShortName();
|
||||
$type = 'string';
|
||||
$schemaType = 'string';
|
||||
if ($re->isBacked() && ($backingType = $re->getBackingType()) && method_exists($backingType, 'getName')) {
|
||||
$type = !Generator::isDefault($schema->type) ? $schema->type : $backingType->getName();
|
||||
if (Generator::isDefault($schema->type)) {
|
||||
$type = $backingType->getName();
|
||||
} else {
|
||||
$type = $schema->type;
|
||||
$schemaType = $schema->type;
|
||||
}
|
||||
}
|
||||
$schema->enum = array_map(function ($case) use ($re, $type) {
|
||||
return $re->isBacked() && $type === 'string' ? $case->getBackingValue() : $case->name;
|
||||
$schema->enum = array_map(function ($case) use ($re, $schemaType, $type) {
|
||||
if ($re->isBacked() && $type === $schemaType) {
|
||||
return $case->getBackingValue();
|
||||
}
|
||||
|
||||
return $case->name;
|
||||
}, $re->getCases());
|
||||
Util::mapNativeType($schema, $type);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace OpenApi\Tests\Fixtures\PHP;
|
||||
|
||||
use OpenApi\Attributes\Schema;
|
||||
|
||||
#[Schema(type: 'integer')]
|
||||
enum StatusEnumIntegerBacked: int
|
||||
{
|
||||
case DRAFT = 1;
|
||||
case PUBLISHED = 2;
|
||||
case ARCHIVED = 3;
|
||||
}
|
|
@ -11,6 +11,7 @@ use OpenApi\Analysers\TokenAnalyser;
|
|||
use OpenApi\Processors\ExpandEnums;
|
||||
use OpenApi\Tests\Fixtures\PHP\StatusEnum;
|
||||
use OpenApi\Tests\Fixtures\PHP\StatusEnumBacked;
|
||||
use OpenApi\Tests\Fixtures\PHP\StatusEnumIntegerBacked;
|
||||
use OpenApi\Tests\Fixtures\PHP\StatusEnumStringBacked;
|
||||
use OpenApi\Tests\OpenApiTestCase;
|
||||
|
||||
|
@ -43,6 +44,15 @@ class ExpandEnumsTest extends OpenApiTestCase
|
|||
self::assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum);
|
||||
}
|
||||
|
||||
public function testExpandBackedIntegerEnum(): void
|
||||
{
|
||||
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumIntegerBacked.php']);
|
||||
$analysis->process([new ExpandEnums()]);
|
||||
$schema = $analysis->getSchemaForSource(StatusEnumIntegerBacked::class);
|
||||
|
||||
self::assertEquals([1, 2, 3], $schema->enum);
|
||||
}
|
||||
|
||||
public function testExpandBackedStringEnum(): void
|
||||
{
|
||||
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumStringBacked.php']);
|
||||
|
|
Loading…
Reference in New Issue