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; 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()); + } +}