Skip to content

Commit cf128d2

Browse files
fix(metadata): get method metadata for BackedEnums
1 parent 8771988 commit cf128d2

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

src/Metadata/Property/Factory/AttributePropertyMetadataFactory.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,11 @@ public function create(string $resourceClass, string $property, array $options =
5959
return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
6060
}
6161

62-
if ($reflectionEnum) {
63-
if ($reflectionEnum->hasCase($property)) {
64-
$reflectionCase = $reflectionEnum->getCase($property);
65-
if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
66-
return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
67-
}
62+
if ($reflectionEnum && $reflectionEnum->hasCase($property)) {
63+
$reflectionCase = $reflectionEnum->getCase($property);
64+
if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
65+
return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
6866
}
69-
70-
return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
7167
}
7268

7369
if ($reflectionClass->hasProperty($property)) {
@@ -79,11 +75,11 @@ public function create(string $resourceClass, string $property, array $options =
7975

8076
foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
8177
$methodName = $prefix.ucfirst($property);
82-
if (!$reflectionClass->hasMethod($methodName)) {
78+
if (!$reflectionClass->hasMethod($methodName) && !$reflectionEnum?->hasMethod($methodName)) {
8379
continue;
8480
}
8581

86-
$reflectionMethod = $reflectionClass->getMethod($methodName);
82+
$reflectionMethod = $reflectionClass->hasMethod($methodName) ? $reflectionClass->getMethod($methodName) : $reflectionEnum?->getMethod($methodName);
8783
if (!$reflectionMethod->isPublic()) {
8884
continue;
8985
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
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\Issue6317;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
19+
#[ApiResource]
20+
enum Issue6317: int
21+
{
22+
case First = 1;
23+
case Second = 2;
24+
25+
#[ApiProperty(identifier: true, example: 'An example of an ID')]
26+
public function getId(): int
27+
{
28+
return $this->value;
29+
}
30+
31+
#[ApiProperty(jsonSchemaContext: ['example' => '/lisa/mary'])]
32+
public function getName(): string
33+
{
34+
return $this->name;
35+
}
36+
37+
#[ApiProperty(jsonldContext: ['example' => '24'])]
38+
public function getOrdinal(): string
39+
{
40+
return 1 === $this->value ? '1st' : '2nd';
41+
}
42+
43+
#[ApiProperty(openapiContext: ['example' => '42'])]
44+
public function getCardinal(): int
45+
{
46+
return $this->value;
47+
}
48+
}

tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,22 @@ public function testJsonApiIncludesSchema(): void
269269
$this->assertArrayHasKey('kingdom', $properties['attributes']['properties']);
270270
$this->assertArrayHasKey('phylum', $properties['attributes']['properties']);
271271
}
272+
273+
/**
274+
* Test issue #6317.
275+
*/
276+
public function testBackedEnumExamplesAreNotLost(): void
277+
{
278+
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6317\Issue6317', '--type' => 'output', '--format' => 'jsonld']);
279+
$result = $this->tester->getDisplay();
280+
$json = json_decode($result, associative: true);
281+
$properties = $json['definitions']['Issue6317.jsonld']['properties'];
282+
283+
$this->assertArrayHasKey('example', $properties['id']);
284+
$this->assertArrayHasKey('example', $properties['name']);
285+
// jsonldContext
286+
$this->assertArrayNotHasKey('example', $properties['ordinal']);
287+
// openapiContext
288+
$this->assertArrayNotHasKey('example', $properties['cardinal']);
289+
}
272290
}

tests/Symfony/Bundle/Command/OpenApiCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ public function testWriteToFile(): void
117117
@unlink($tmpFile);
118118
}
119119

120+
/**
121+
* Test issue #6317.
122+
*/
123+
public function testBackedEnumExamplesAreNotLost(): void
124+
{
125+
$this->tester->run(['command' => 'api:openapi:export']);
126+
$result = $this->tester->getDisplay();
127+
$json = json_decode($result, true, 512, \JSON_THROW_ON_ERROR);
128+
129+
$assertExample = function (array $properties, string $id): void {
130+
$this->assertArrayHasKey('example', $properties[$id]); // default
131+
$this->assertArrayHasKey('example', $properties['cardinal']); // openapiContext
132+
$this->assertArrayNotHasKey('example', $properties['name']); // jsonSchemaContext
133+
$this->assertArrayNotHasKey('example', $properties['ordinal']); // jsonldContext
134+
};
135+
136+
$assertExample($json['components']['schemas']['Issue6317']['properties'], 'id');
137+
$assertExample($json['components']['schemas']['Issue6317.jsonld']['properties'], 'id');
138+
$assertExample($json['components']['schemas']['Issue6317.jsonapi']['properties']['data']['properties']['attributes']['properties'], '_id');
139+
$assertExample($json['components']['schemas']['Issue6317.jsonhal']['properties'], 'id');
140+
}
141+
120142
private function assertYaml(string $data): void
121143
{
122144
try {

0 commit comments

Comments
 (0)