Skip to content

Commit debd828

Browse files
test: resource with enum properties schema
1 parent b28eda8 commit debd828

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Tests\Fixtures\TestBundle\ApiResource;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\GetCollection;
19+
use ApiPlatform\Metadata\Operation;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\Enum\GenderTypeEnum;
21+
22+
#[ApiResource()]
23+
#[Get(
24+
provider: self::class.'::providerItem',
25+
)]
26+
#[GetCollection(
27+
provider: self::class.'::providerCollection',
28+
)]
29+
class ResourceWithEnumProperty
30+
{
31+
public int $id = 1;
32+
33+
public ?BackedEnumIntegerResource $intEnum = null;
34+
35+
/** @var BackedEnumStringResource[] */
36+
public array $stringEnum = [];
37+
38+
public ?GenderTypeEnum $gender = null;
39+
40+
/** @var GenderTypeEnum[] */
41+
public array $genders = [];
42+
43+
public static function providerItem(Operation $operation, array $uriVariables): self
44+
{
45+
$self = new self();
46+
$self->intEnum = BackedEnumIntegerResource::Yes;
47+
$self->stringEnum = [BackedEnumIntegerResource::Maybe, BackedEnumIntegerResource::No];
48+
$self->gender = GenderTypeEnum::FEMALE;
49+
$self->genders = [GenderTypeEnum::FEMALE, GenderTypeEnum::MALE];
50+
51+
return $self;
52+
}
53+
54+
public static function providerCollection(Operation $operation, array $uriVariables): array
55+
{
56+
return [self::providerItem($operation, $uriVariables)];
57+
}
58+
}

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,49 @@ public function testBackedEnumExamplesAreNotLost(): void
287287
// openapiContext
288288
$this->assertArrayNotHasKey('example', $properties['cardinal']);
289289
}
290+
291+
public function testResourceWithEnumPropertiesSchema(): void
292+
{
293+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ResourceWithEnumProperty', '--type' => 'output', '--format' => 'jsonld']);
294+
$result = $this->tester->getDisplay();
295+
$json = json_decode($result, associative: true);
296+
$properties = $json['definitions']['ResourceWithEnumProperty.jsonld']['properties'];
297+
298+
$this->assertSame(
299+
[
300+
'type' => ['string', 'null'],
301+
'format' => 'iri-reference',
302+
'example' => 'https://example.com/',
303+
],
304+
$properties['intEnum']
305+
);
306+
$this->assertSame(
307+
[
308+
'type' => 'array',
309+
'items' => [
310+
'type' => 'string',
311+
'format' => 'iri-reference',
312+
'example' => 'https://example.com/',
313+
],
314+
],
315+
$properties['stringEnum']
316+
);
317+
$this->assertSame(
318+
[
319+
'type' => ['string', 'null'],
320+
'enum' => ['male', 'female', null],
321+
],
322+
$properties['gender']
323+
);
324+
$this->assertSame(
325+
[
326+
'type' => 'array',
327+
'items' => [
328+
'type' => 'string',
329+
'enum' => ['male', 'female'],
330+
],
331+
],
332+
$properties['genders']
333+
);
334+
}
290335
}

0 commit comments

Comments
 (0)