Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Multiple Enums class-names in Schema::enum property #1460

Merged
merged 4 commits into from
Aug 3, 2023
Merged
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
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