Skip to content

Commit 2a43268

Browse files
sarimsoyuka
andauthored
fix(jsonschema): fix invalid "int" type to "integer" (#6049)
* 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" * test: enum schema as integer --------- Co-authored-by: soyuka <[email protected]>
1 parent 41e39a6 commit 2a43268

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private function getClassType(?string $className, bool $nullable, ?bool $readabl
244244
if (!$this->isResourceClass($className) && is_a($className, \BackedEnum::class, true)) {
245245
$enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());
246246

247-
$type = \is_string($enumCases[0] ?? '') ? 'string' : 'int';
247+
$type = \is_string($enumCases[0] ?? '') ? 'string' : 'integer';
248248

249249
if ($nullable) {
250250
$enumCases[] = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonSchema\Tests\Fixtures;
15+
16+
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
17+
use ApiPlatform\Metadata\ApiResource;
18+
19+
/*
20+
* This file is part of the API Platform project.
21+
*
22+
* (c) Kévin Dunglas <[email protected]>
23+
*
24+
* For the full copyright and license information, please view the LICENSE
25+
* file that was distributed with this source code.
26+
*/
27+
28+
#[ApiResource]
29+
class DummyWithEnum
30+
{
31+
public $id;
32+
33+
public function __construct(
34+
public IntEnumAsIdentifier $intEnumAsIdentifier = IntEnumAsIdentifier::FOO,
35+
) {
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonSchema\Tests\Fixtures\Enum;
15+
16+
enum IntEnumAsIdentifier: int
17+
{
18+
case FOO = 1;
19+
case BAR = 2;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonSchema\Tests\Metadata\Property\Factory;
15+
16+
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17+
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithEnum;
18+
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
19+
use ApiPlatform\Metadata\ApiProperty;
20+
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21+
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22+
use PHPUnit\Framework\TestCase;
23+
use Symfony\Component\PropertyInfo\Type;
24+
25+
class SchemaPropertyMetadataFactoryTest extends TestCase
26+
{
27+
public function testEnum(): void
28+
{
29+
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
30+
$apiProperty = new ApiProperty(builtinTypes: [new Type(builtinType: 'object', nullable: true, class: IntEnumAsIdentifier::class)]);
31+
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
32+
$decorated->expects($this->once())->method('create')->with(DummyWithEnum::class, 'intEnumAsIdentifier')->willReturn($apiProperty);
33+
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
34+
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier');
35+
$this->assertEquals(['type' => ['integer', 'null'], 'enum' => [1, 2, null]], $apiProperty->getSchema());
36+
}
37+
}

0 commit comments

Comments
 (0)