diff --git a/tests/Fixtures/TestBundle/ApiResource/ResourceWithEnumProperty.php b/tests/Fixtures/TestBundle/ApiResource/ResourceWithEnumProperty.php new file mode 100644 index 0000000000..1cb3ac6661 --- /dev/null +++ b/tests/Fixtures/TestBundle/ApiResource/ResourceWithEnumProperty.php @@ -0,0 +1,58 @@ + + * + * 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\Tests\Fixtures\TestBundle\ApiResource; + +use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\Get; +use ApiPlatform\Metadata\GetCollection; +use ApiPlatform\Metadata\Operation; +use ApiPlatform\Tests\Fixtures\TestBundle\Enum\GenderTypeEnum; + +#[ApiResource()] +#[Get( + provider: self::class.'::providerItem', +)] +#[GetCollection( + provider: self::class.'::providerCollection', +)] +class ResourceWithEnumProperty +{ + public int $id = 1; + + public ?BackedEnumIntegerResource $intEnum = null; + + /** @var BackedEnumStringResource[] */ + public array $stringEnum = []; + + public ?GenderTypeEnum $gender = null; + + /** @var GenderTypeEnum[] */ + public array $genders = []; + + public static function providerItem(Operation $operation, array $uriVariables): self + { + $self = new self(); + $self->intEnum = BackedEnumIntegerResource::Yes; + $self->stringEnum = [BackedEnumIntegerResource::Maybe, BackedEnumIntegerResource::No]; + $self->gender = GenderTypeEnum::FEMALE; + $self->genders = [GenderTypeEnum::FEMALE, GenderTypeEnum::MALE]; + + return $self; + } + + public static function providerCollection(Operation $operation, array $uriVariables): array + { + return [self::providerItem($operation, $uriVariables)]; + } +} diff --git a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php index 0d0aef92dc..67248042f5 100644 --- a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php +++ b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php @@ -287,4 +287,49 @@ public function testBackedEnumExamplesAreNotLost(): void // openapiContext $this->assertArrayNotHasKey('example', $properties['cardinal']); } + + public function testResourceWithEnumPropertiesSchema(): void + { + $this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ResourceWithEnumProperty', '--type' => 'output', '--format' => 'jsonld']); + $result = $this->tester->getDisplay(); + $json = json_decode($result, associative: true); + $properties = $json['definitions']['ResourceWithEnumProperty.jsonld']['properties']; + + $this->assertSame( + [ + 'type' => ['string', 'null'], + 'format' => 'iri-reference', + 'example' => 'https://example.com/', + ], + $properties['intEnum'] + ); + $this->assertSame( + [ + 'type' => 'array', + 'items' => [ + 'type' => 'string', + 'format' => 'iri-reference', + 'example' => 'https://example.com/', + ], + ], + $properties['stringEnum'] + ); + $this->assertSame( + [ + 'type' => ['string', 'null'], + 'enum' => ['male', 'female', null], + ], + $properties['gender'] + ); + $this->assertSame( + [ + 'type' => 'array', + 'items' => [ + 'type' => 'string', + 'enum' => ['male', 'female'], + ], + ], + $properties['genders'] + ); + } }