-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <soyuka@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/JsonSchema/Tests/Fixtures/Enum/IntEnumAsIdentifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |