From b7d2f90de562e08008e0c85d1c7e5d1ea0154eef Mon Sep 17 00:00:00 2001 From: DidierLmn Date: Thu, 27 Jul 2023 09:45:37 +0000 Subject: [PATCH 1/7] Fix enum always marked as string type --- ModelDescriber/EnumModelDescriber.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ModelDescriber/EnumModelDescriber.php b/ModelDescriber/EnumModelDescriber.php index 41fba6e42..b1cc18fb4 100644 --- a/ModelDescriber/EnumModelDescriber.php +++ b/ModelDescriber/EnumModelDescriber.php @@ -17,6 +17,12 @@ public function describe(Model $model, Schema $schema) $enums[] = $enumCase->value; } + $reflectionEnum = new \ReflectionEnum($enumClass); + if ($reflectionEnum->isBacked() && $reflectionEnum->getBackingType()->getName() === 'int') { + $schema->type = 'integer'; + } else { + $schema->type = 'string'; + } $schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) ? 'int' : 'string'; $schema->enum = $enums; } From 8d08f0f8c5894ec97b496a6475b53a0ce8bef377 Mon Sep 17 00:00:00 2001 From: DidierLmn Date: Thu, 27 Jul 2023 09:57:05 +0000 Subject: [PATCH 2/7] Add tests for int backed enum and enum without backing --- Tests/Functional/Entity/ArticleType81IntBacked.php | 9 +++++++++ Tests/Functional/Entity/ArticleType81NotBacked.php | 9 +++++++++ Tests/Functional/FunctionalTest.php | 11 +++++++++++ 3 files changed, 29 insertions(+) create mode 100644 Tests/Functional/Entity/ArticleType81IntBacked.php create mode 100644 Tests/Functional/Entity/ArticleType81NotBacked.php diff --git a/Tests/Functional/Entity/ArticleType81IntBacked.php b/Tests/Functional/Entity/ArticleType81IntBacked.php new file mode 100644 index 000000000..1d1f243d8 --- /dev/null +++ b/Tests/Functional/Entity/ArticleType81IntBacked.php @@ -0,0 +1,9 @@ +assertSame('string', $model->type); $this->assertCount(2, $model->enum); + + $model = $this->getModel('ArticleType81NotBacked'); + + $this->assertSame('string', $model->type); + $this->assertCount(2, $model->enum); + + + $model = $this->getModel('ArticleType81IntBacked'); + + $this->assertSame('integer', $model->type); + $this->assertCount(2, $model->enum); } public function testEntitiesWithOverriddenSchemaTypeDoNotReadOtherProperties() From 47d156ac68dae90ade2e48e832cd9abe8ded5524 Mon Sep 17 00:00:00 2001 From: Didier Laumen Date: Thu, 12 Oct 2023 15:54:01 +0200 Subject: [PATCH 3/7] Remove duplicate type setting --- ModelDescriber/EnumModelDescriber.php | 1 - 1 file changed, 1 deletion(-) diff --git a/ModelDescriber/EnumModelDescriber.php b/ModelDescriber/EnumModelDescriber.php index b1cc18fb4..965d3839c 100644 --- a/ModelDescriber/EnumModelDescriber.php +++ b/ModelDescriber/EnumModelDescriber.php @@ -23,7 +23,6 @@ public function describe(Model $model, Schema $schema) } else { $schema->type = 'string'; } - $schema->type = is_subclass_of($enumClass, \IntBackedEnum::class) ? 'int' : 'string'; $schema->enum = $enums; } From 855308e0613dba7707f217380ecdb5f4e4b64cee Mon Sep 17 00:00:00 2001 From: Didier Laumen Date: Fri, 13 Oct 2023 10:39:31 +0200 Subject: [PATCH 4/7] Style fixes --- ModelDescriber/EnumModelDescriber.php | 2 +- Tests/Functional/FunctionalTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ModelDescriber/EnumModelDescriber.php b/ModelDescriber/EnumModelDescriber.php index 965d3839c..93ad24fb1 100644 --- a/ModelDescriber/EnumModelDescriber.php +++ b/ModelDescriber/EnumModelDescriber.php @@ -18,7 +18,7 @@ public function describe(Model $model, Schema $schema) } $reflectionEnum = new \ReflectionEnum($enumClass); - if ($reflectionEnum->isBacked() && $reflectionEnum->getBackingType()->getName() === 'int') { + if ($reflectionEnum->isBacked() && 'int' === $reflectionEnum->getBackingType()->getName()) { $schema->type = 'integer'; } else { $schema->type = 'string'; diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 4af14588d..53ec17f60 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -665,7 +665,6 @@ public function testEnumSupport() $this->assertSame('string', $model->type); $this->assertCount(2, $model->enum); - $model = $this->getModel('ArticleType81IntBacked'); $this->assertSame('integer', $model->type); From b7fd28a8449e23c19e0a54346448881da4872a2e Mon Sep 17 00:00:00 2001 From: Didier Laumen Date: Fri, 13 Oct 2023 10:44:54 +0200 Subject: [PATCH 5/7] Fix wrong enum name --- Tests/Functional/Entity/ArticleType81NotBacked.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Functional/Entity/ArticleType81NotBacked.php b/Tests/Functional/Entity/ArticleType81NotBacked.php index 16a7e37d3..a274ed8a2 100644 --- a/Tests/Functional/Entity/ArticleType81NotBacked.php +++ b/Tests/Functional/Entity/ArticleType81NotBacked.php @@ -2,7 +2,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; -enum ArticleType81IntBacked +enum ArticleType81NotBacked { case DRAFT; case FINAL; From c6bfa9b8e287f42838d383d755e03f9c983af077 Mon Sep 17 00:00:00 2001 From: Didier Laumen Date: Fri, 13 Oct 2023 10:53:35 +0200 Subject: [PATCH 6/7] Attempt test fix --- Tests/Functional/Entity/Article81.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/Functional/Entity/Article81.php b/Tests/Functional/Entity/Article81.php index 390480183..8a483cd79 100644 --- a/Tests/Functional/Entity/Article81.php +++ b/Tests/Functional/Entity/Article81.php @@ -7,6 +7,8 @@ class Article81 public function __construct( public readonly int $id, public readonly ArticleType81 $type, + public readonly ArticleType81IntBacked $intBackedType, + public readonly ArticleType81NotBacked $notBackedType, ) { } } From b6463d488c2aeaf2e0f36811aa1558e26a4c13fb Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Tue, 2 Jan 2024 14:27:32 +0100 Subject: [PATCH 7/7] fix test for non backed enum --- Tests/Functional/FunctionalTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 574bc929b..0498fff01 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -681,8 +681,7 @@ public function testEnumSupport() $model = $this->getModel('ArticleType81NotBacked'); - $this->assertSame('string', $model->type); - $this->assertCount(2, $model->enum); + $this->assertSame('object', $model->type, 'Non backed enums cannot be described'); $model = $this->getModel('ArticleType81IntBacked');