Skip to content

Commit

Permalink
Create an enum model describer (#1965)
Browse files Browse the repository at this point in the history
* Create an enum model describer

* Bump Api-Platform

Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
  • Loading branch information
magnetik and GuilhemN committed Apr 4, 2022
1 parent 2295f68 commit 1302bc7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
34 changes: 34 additions & 0 deletions ModelDescriber/EnumModelDescriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Nelmio\ApiDocBundle\ModelDescriber;

use Nelmio\ApiDocBundle\Model\Model;
use OpenApi\Annotations\Schema;
use Symfony\Component\PropertyInfo\Type;

class EnumModelDescriber implements ModelDescriberInterface
{
public function describe(Model $model, Schema $schema)
{
$enumClass = $model->getType()->getClassName();

$enums = [];
foreach ($enumClass::cases() as $enumCase) {
$enums[] = $enumCase->value;
}

$schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) ? 'int' : 'string';
$schema->enum = $enums;
}

public function supports(Model $model): bool
{
if (!function_exists('enum_exists')) {
return false;
}

return Type::BUILTIN_TYPE_OBJECT === $model->getType()->getBuiltinType()
&& enum_exists($model->getType()->getClassName())
&& is_subclass_of($model->getType()->getClassName(), \BackedEnum::class);
}
}
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
<tag name="nelmio_api_doc.model_describer" />
</service>

<service id="nelmio_api_doc.model_describers.enum" class="Nelmio\ApiDocBundle\ModelDescriber\EnumModelDescriber" public="false">
<tag name="nelmio_api_doc.model_describer" priority="100"/>
</service>

<service id="nelmio_api_doc.model_describers.object_fallback" class="Nelmio\ApiDocBundle\ModelDescriber\FallbackObjectModelDescriber" public="false">
<tag name="nelmio_api_doc.model_describer" priority="-1000" />
</service>
Expand Down
7 changes: 7 additions & 0 deletions Tests/Functional/Controller/ApiController81.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Nelmio\ApiDocBundle\Annotation\Model;
use Nelmio\ApiDocBundle\Annotation\Security;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article;
use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article81;
use OpenApi\Attributes as OA;
use Symfony\Component\Routing\Annotation\Route;

Expand Down Expand Up @@ -65,4 +66,10 @@ public function inlinePathParameters(
#[OA\PathParameter] string $product_id
) {
}

#[Route('/enum')]
#[OA\Response(response: '201', description: '', attachables: [new Model(type: Article81::class)])]
public function enum()
{
}
}
12 changes: 12 additions & 0 deletions Tests/Functional/Entity/Article81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;

class Article81
{
public function __construct(
public readonly int $id,
public readonly ArticleType81 $type,
) {
}
}
9 changes: 9 additions & 0 deletions Tests/Functional/Entity/ArticleType81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Nelmio\ApiDocBundle\Tests\Functional\Entity;

enum ArticleType81: string
{
case DRAFT = 'draft';
case FINAL = 'final';
}
11 changes: 11 additions & 0 deletions Tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,4 +624,15 @@ public function testNoAdditionalPropertiesSupport()

$this->assertFalse($model->additionalProperties);
}

/**
* @requires PHP >= 8.1
*/
public function testEnumSupport()
{
$model = $this->getModel('ArticleType81');

$this->assertSame('string', $model->type);
$this->assertCount(2, $model->enum);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"symfony/twig-bundle": "^4.4|^5.2|^6.0",
"symfony/validator": "^4.4|^5.2|^6.0",

"api-platform/core": "^2.4",
"api-platform/core": "^2.6.8",
"friendsofsymfony/rest-bundle": "^2.8|^3.0",
"willdurand/hateoas-bundle": "^1.0|^2.0",
"jms/serializer-bundle": "^2.3|^3.0|^4.0",
Expand Down

0 comments on commit 1302bc7

Please sign in to comment.