Skip to content

Commit

Permalink
Support Multiple Enums class-names in Schema::enum property (zircote#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vad0s94 authored Aug 3, 2023
1 parent 7e583e1 commit 048ea9d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Processors/ExpandEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,19 @@ protected function expandSchemaEnum(Analysis $analysis): void
} else {
// might be an array of \UnitEnum::class, string, int, etc...
assert(is_array($schema->enum));
$cases = $schema->enum;

$cases = [];

// transform each Enum cases into UnitEnum
foreach ($schema->enum as $enum) {
if (is_string($enum) && enum_exists($enum)) {
foreach ($enum::cases() as $case) {
$cases[] = $case;
}
} else {
$cases[] = $enum;
}
}
}

$enums = [];
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixtures/PHP/Enums/TypeEnumStringBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\PHP\Enums;

use OpenApi\Attributes as OAT;

#[OAT\Schema(type: 'string')]
enum TypeEnumStringBacked: string
{
case FEATURE = 'feature';
case BUG = 'bug';
case IMPROVEMENT = 'improvement';
}
17 changes: 17 additions & 0 deletions tests/Fixtures/PHP/ReferencesEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumIntegerBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumStringBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\TypeEnumStringBacked;

#[OAT\Schema()]
class ReferencesEnum
Expand Down Expand Up @@ -51,6 +52,22 @@ class ReferencesEnum
#[OAT\Property(title: 'statusEnumStringBackedMixed', description: 'Status enum string backed mixed', type: 'string', enum: [StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other'], nullable: true)]
public ?string $statusEnumStringBackedMixed;

#[OAT\Property(
title: 'multipleEnumsClassNames',
description: 'MultipleEnumsClassNames array',
type: 'string',
enum: [StatusEnumStringBacked::class, TypeEnumStringBacked::class],
)]
public string $multipleEnumsClassNames;

#[OAT\Property(
title: 'mixedMultipleEnumsClassNames',
description: 'MultipleEnumsClassNames array',
type: 'string',
enum: [StatusEnumStringBacked::class, TypeEnumStringBacked::class, 'OTHER'],
)]
public string $mixedMultipleEnumsClassNames;

/** @var list<string> StatusEnumStringBacked array */
#[OAT\Property(
title: 'statusEnums',
Expand Down
11 changes: 11 additions & 0 deletions tests/Processors/ExpandEnumsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumIntegerBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnumStringBacked;
use OpenApi\Tests\Fixtures\PHP\Enums\TypeEnumStringBacked;
use OpenApi\Tests\OpenApiTestCase;

class ExpandEnumsTest extends OpenApiTestCase
Expand Down Expand Up @@ -136,6 +137,16 @@ public function expandEnumClassStringFixtures(): iterable
'itemsStatusEnumStringBackedMixed',
$mapValues([StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']),
],
'multipleEnumsClassNames' => [
['PHP/ReferencesEnum.php'],
'multipleEnumsClassNames',
$mapValues(array_merge(StatusEnumStringBacked::cases(), TypeEnumStringBacked::cases())),
],
'mixedMultipleEnumsClassNames' => [
['PHP/ReferencesEnum.php'],
'mixedMultipleEnumsClassNames',
$mapValues(array_merge(StatusEnumStringBacked::cases(), TypeEnumStringBacked::cases(), ['OTHER'])),
],
];
}

Expand Down

0 comments on commit 048ea9d

Please sign in to comment.