Skip to content

Commit

Permalink
get string when backed enum type is string
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed May 4, 2022
1 parent 2b90a4d commit c0195a1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Processors/ExpandEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function __invoke(Analysis $analysis)
$source = $schema->_context->enum;
$re = new \ReflectionEnum($schema->_context->fullyQualifiedName($source));
$schema->schema = !Generator::isDefault($schema->schema) ? $schema->schema : $re->getShortName();
$schema->enum = array_map(function ($case) {
return $case->name;
}, $re->getCases());
$type = 'string';
if ($re->isBacked() && ($backingType = $re->getBackingType()) && method_exists($backingType, 'getName')) {
$type = !Generator::isDefault($schema->type) ? $schema->type : $backingType->getName();
}
$schema->enum = array_map(function ($case) use ($re, $type) {
return $re->isBacked() && $type === 'string' ? $case->getBackingValue() : $case->name;
}, $re->getCases());
Util::mapNativeType($schema, $type);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/PHP/StatusEnumStringBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace OpenApi\Tests\Fixtures\PHP;

use OpenApi\Attributes\Schema;

#[Schema()]
enum StatusEnumStringBacked: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';
case ARCHIVED = 'archived';
}
32 changes: 28 additions & 4 deletions tests/Processors/ExpandEnumsTest.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Processors;

use OpenApi\Analysers\AttributeAnnotationFactory;
use OpenApi\Analysers\DocBlockAnnotationFactory;
use OpenApi\Analysers\ReflectionAnalyser;
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\StatusEnumStringBacked;
use OpenApi\Tests\OpenApiTestCase;

class ExpandEnumsTest extends OpenApiTestCase
{
public function setUp(): void
{
parent::setUp();
if (! class_exists('\\ReflectionEnum')) {
if ($this->getAnalyzer() instanceof TokenAnalyser) {
$this->markTestSkipped();
}
}

public function testExpandUnitEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnum.php']);
$analysis = $this->analysisFromFixtures(['PHP/StatusEnum.php'], [], $this->getAnalyzer());
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnum::class);

self::assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum);
}
}

public function testExpandBackedEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumBacked.php'], [], $this->getAnalyzer());
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnumBacked::class);

self::assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum);
}

public function testExpandBackedStringEnum(): void
{
$analysis = $this->analysisFromFixtures(['PHP/StatusEnumStringBacked.php'], [], $this->getAnalyzer());
$analysis->process([new ExpandEnums()]);
$schema = $analysis->getSchemaForSource(StatusEnumStringBacked::class);

self::assertEquals(['draft', 'published', 'archived'], $schema->enum);
}
}

0 comments on commit c0195a1

Please sign in to comment.