Skip to content

Commit

Permalink
fix(metadata): get method metadata for BackedEnums
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendolenLynch committed Apr 19, 2024
1 parent 3024ba7 commit 3df9f0b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/Metadata/Property/Factory/AttributePropertyMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,11 @@ public function create(string $resourceClass, string $property, array $options =
return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
}

if ($reflectionEnum) {
if ($reflectionEnum->hasCase($property)) {
$reflectionCase = $reflectionEnum->getCase($property);
if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
}
if ($reflectionEnum && $reflectionEnum->hasCase($property)) {
$reflectionCase = $reflectionEnum->getCase($property);
if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
}

return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
}

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

foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
$methodName = $prefix.ucfirst($property);
if (!$reflectionClass->hasMethod($methodName)) {
if (!$reflectionClass->hasMethod($methodName) && !$reflectionEnum?->hasMethod($methodName)) {
continue;
}

$reflectionMethod = $reflectionClass->getMethod($methodName);
$reflectionMethod = $reflectionClass->hasMethod($methodName) ? $reflectionClass->getMethod($methodName) : $reflectionEnum?->getMethod($methodName);
if (!$reflectionMethod->isPublic()) {
continue;
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/Issue6317/Issue6317.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Tests\Fixtures\TestBundle\ApiResource\Issue6317;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;

#[ApiResource]
enum Issue6317: int
{
case First = 1;
case Second = 2;

#[ApiProperty(identifier: true, example: 'An example of an ID')]
public function getId(): int
{
return $this->value;
}

#[ApiProperty(jsonSchemaContext: ['example' => '/lisa/mary'])]
public function getName(): string
{
return $this->name;
}

#[ApiProperty(jsonldContext: ['example' => '24'])]
public function getOrdinal(): string
{
return 1 === $this->value ? '1st' : '2nd';
}

#[ApiProperty(openapiContext: ['example' => '42'])]
public function getCardinal(): int
{
return $this->value;
}
}
18 changes: 18 additions & 0 deletions tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,22 @@ public function testJsonApiIncludesSchema(): void
$this->assertArrayHasKey('$ref', $json['definitions']['Question.jsonapi']['properties']['included']['items']['anyOf'][0]);
$this->assertSame('#/definitions/Answer.jsonapi', $json['definitions']['Question.jsonapi']['properties']['included']['items']['anyOf'][0]['$ref']);
}

/**
* Test issue #6317.
*/
public function testBackedEnumExamplesAreNotLost(): void
{
$this->tester->run(['command' => 'api:json-schema:generate', 'resource' => 'ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6317\Issue6317', '--type' => 'output', '--format' => 'jsonld']);
$result = $this->tester->getDisplay();
$json = json_decode($result, associative: true);
$properties = $json['definitions']['Issue6317.jsonld']['properties'];

$this->assertArrayHasKey('example', $properties['id']);
$this->assertArrayHasKey('example', $properties['name']);
// jsonldContext
$this->assertArrayNotHasKey('example', $properties['ordinal']);
// openapiContext
$this->assertArrayNotHasKey('example', $properties['cardinal']);
}
}
22 changes: 22 additions & 0 deletions tests/Symfony/Bundle/Command/OpenApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ public function testWriteToFile(): void
@unlink($tmpFile);
}

/**
* Test issue #6317.
*/
public function testBackedEnumExamplesAreNotLost(): void
{
$this->tester->run(['command' => 'api:openapi:export']);
$result = $this->tester->getDisplay();
$json = json_decode($result, true, 512, \JSON_THROW_ON_ERROR);

$assertExample = function (array $properties, string $id): void {
$this->assertArrayHasKey('example', $properties[$id]); // default
$this->assertArrayHasKey('example', $properties['cardinal']); // openapiContext
$this->assertArrayNotHasKey('example', $properties['name']); // jsonSchemaContext
$this->assertArrayNotHasKey('example', $properties['ordinal']); // jsonldContext
};

$assertExample($json['components']['schemas']['Issue6317']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonld']['properties'], 'id');
$assertExample($json['components']['schemas']['Issue6317.jsonapi']['properties']['data']['properties']['attributes']['properties'], '_id');
$assertExample($json['components']['schemas']['Issue6317.jsonhal']['properties'], 'id');
}

private function assertYaml(string $data): void
{
try {
Expand Down

0 comments on commit 3df9f0b

Please sign in to comment.