Skip to content

Commit

Permalink
fix(jsonschema): fix invalid "int" type to "integer" (#6049)
Browse files Browse the repository at this point in the history
* 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 <soyuka@users.noreply.github.com>
  • Loading branch information
sarim and soyuka authored Dec 18, 2023
1 parent 41e39a6 commit 2a43268
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions src/JsonSchema/Tests/Fixtures/DummyWithEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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 <dunglas@gmail.com>
*
* 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,
) {
}
}
20 changes: 20 additions & 0 deletions src/JsonSchema/Tests/Fixtures/Enum/IntEnumAsIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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());
}
}

0 comments on commit 2a43268

Please sign in to comment.