From 1baf125304a02813bc91799146b59e084da3d2e7 Mon Sep 17 00:00:00 2001 From: Sarim Khan Date: Fri, 15 Dec 2023 07:40:42 +0600 Subject: [PATCH 1/2] fix(jsonschema): fix invalid "int" type to "integer" Fixes: 6048 For int backed enum, api-platform generates "type":"int", which is invalid in jsonschema. It should be "integer" --- .../Metadata/Property/Factory/SchemaPropertyMetadataFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php b/src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php index 003f4cf1902..62b80d86a43 100644 --- a/src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php +++ b/src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php @@ -244,7 +244,7 @@ private function getClassType(?string $className, bool $nullable, ?bool $readabl if (!$this->isResourceClass($className) && is_a($className, \BackedEnum::class, true)) { $enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases()); - $type = \is_string($enumCases[0] ?? '') ? 'string' : 'int'; + $type = \is_string($enumCases[0] ?? '') ? 'string' : 'integer'; if ($nullable) { $enumCases[] = null; From 8f50a16873afdbf5754ddc6ca2b705f1207aaca9 Mon Sep 17 00:00:00 2001 From: soyuka Date: Mon, 18 Dec 2023 10:57:23 +0100 Subject: [PATCH 2/2] test: enum schema as integer --- .../Tests/Fixtures/DummyWithEnum.php | 37 +++++++++++++++++++ .../Fixtures/Enum/IntEnumAsIdentifier.php | 20 ++++++++++ .../SchemaPropertyMetadataFactoryTest.php | 37 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/JsonSchema/Tests/Fixtures/DummyWithEnum.php create mode 100644 src/JsonSchema/Tests/Fixtures/Enum/IntEnumAsIdentifier.php create mode 100644 src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php diff --git a/src/JsonSchema/Tests/Fixtures/DummyWithEnum.php b/src/JsonSchema/Tests/Fixtures/DummyWithEnum.php new file mode 100644 index 00000000000..70acff50b80 --- /dev/null +++ b/src/JsonSchema/Tests/Fixtures/DummyWithEnum.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\JsonSchema\Tests\Fixtures; + +use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier; +use ApiPlatform\Metadata\ApiResource; + +/* + * This file is part of the API Platform project. + * + * (c) Kévin Dunglas + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +#[ApiResource] +class DummyWithEnum +{ + public $id; + + public function __construct( + public IntEnumAsIdentifier $intEnumAsIdentifier = IntEnumAsIdentifier::FOO, + ) { + } +} diff --git a/src/JsonSchema/Tests/Fixtures/Enum/IntEnumAsIdentifier.php b/src/JsonSchema/Tests/Fixtures/Enum/IntEnumAsIdentifier.php new file mode 100644 index 00000000000..27195c80f94 --- /dev/null +++ b/src/JsonSchema/Tests/Fixtures/Enum/IntEnumAsIdentifier.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\JsonSchema\Tests\Fixtures\Enum; + +enum IntEnumAsIdentifier: int +{ + case FOO = 1; + case BAR = 2; +} diff --git a/src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php b/src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php new file mode 100644 index 00000000000..03353150ad8 --- /dev/null +++ b/src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\JsonSchema\Tests\Metadata\Property\Factory; + +use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory; +use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithEnum; +use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier; +use ApiPlatform\Metadata\ApiProperty; +use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; +use ApiPlatform\Metadata\ResourceClassResolverInterface; +use PHPUnit\Framework\TestCase; +use Symfony\Component\PropertyInfo\Type; + +class SchemaPropertyMetadataFactoryTest extends TestCase +{ + public function testEnum(): void + { + $resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class); + $apiProperty = new ApiProperty(builtinTypes: [new Type(builtinType: 'object', nullable: true, class: IntEnumAsIdentifier::class)]); + $decorated = $this->createMock(PropertyMetadataFactoryInterface::class); + $decorated->expects($this->once())->method('create')->with(DummyWithEnum::class, 'intEnumAsIdentifier')->willReturn($apiProperty); + $schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated); + $apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier'); + $this->assertEquals(['type' => ['integer', 'null'], 'enum' => [1, 2, null]], $apiProperty->getSchema()); + } +}