Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/Metadata/IdentifiersExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public function getIdentifiersFromItem(object $item, ?Operation $operation = nul
return $this->getIdentifiersFromOperation($item, $operation, $context);
}

/**
* @param array<string, mixed> $context
*/
private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
{
if ($operation instanceof HttpOperation) {
Expand All @@ -75,24 +78,26 @@ private function getIdentifiersFromOperation(object $item, Operation $operation,
if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
$compositeIdentifiers = [];
foreach ($link->getIdentifiers() as $identifier) {
$compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName());
$compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName(), null, $context, $operation);
}

$identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
continue;
}

$parameterName = $link->getParameterName();
$identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty());
$identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty(), $context, $operation);
}

return $identifiers;
}

/**
* Gets the value of the given class property.
*
* @param array<string, mixed> $context
*/
private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, ?string $toProperty = null): float|bool|int|string
private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, ?string $toProperty, array $context, Operation $operation): float|bool|int|string
{
if ($item instanceof $class) {
try {
Expand All @@ -102,6 +107,15 @@ private function getIdentifierValue(object $item, string $class, string $propert
}
}

// ItemUriTemplate is defined on a collection and we read the identifier alghough the PHP class may be different
if (isset($context['item_uri_template']) && $operation->getClass() === $class) {
try {
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
} catch (NoSuchPropertyException $e) {
throw new RuntimeException(\sprintf('Could not retrieve identifier "%s" for class "%s" using itemUriTemplate "%s". Check that the property exists and is accessible.', $property, $class, $context['item_uri_template']), $e->getCode(), $e);
}
}

if ($toProperty) {
return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
}
Expand Down
41 changes: 40 additions & 1 deletion tests/Functional/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\ImageModuleResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\PageResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7298\TitleModuleResource;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ItemUriTemplateWithCollection\Recipe;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ItemUriTemplateWithCollection\RecipeCollection;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo;
use ApiPlatform\Tests\SetupClassResourcesTrait;
Expand All @@ -39,7 +41,20 @@
*/
public static function getResources(): array
{
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class, PageResource::class, TitleModuleResource::class, ImageModuleResource::class];
return [
Foo::class,
Bar::class,
JsonLdContextOutput::class,
GenIdFalse::class,
AggregateRating::class,
LevelFirst::class,
LevelThird::class,
PageResource::class,
TitleModuleResource::class,
ImageModuleResource::class,
Recipe::class,

Check failure on line 55 in tests/Functional/JsonLdTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Class ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ItemUriTemplateWithCollection\Recipe not found.
RecipeCollection::class,

Check failure on line 56 in tests/Functional/JsonLdTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Class ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ItemUriTemplateWithCollection\RecipeCollection not found.
];
}

/**
Expand Down Expand Up @@ -129,6 +144,30 @@
]);
}

public function testItemUriTemplate(): void
{
self::createClient()->request(
'GET',
'/item_uri_template_recipes',
);
$this->assertResponseIsSuccessful();

$this->assertJsonContains([
'member' => [
[
'@type' => 'RecipeCollection',
'@id' => '/item_uri_template_recipes/1',
'name' => 'Dummy Recipe',
],
[
'@type' => 'RecipeCollection',
'@id' => '/item_uri_template_recipes/2',
'name' => 'Dummy Recipe 2',
],
],
]);
}

protected function setUp(): void
{
self::bootKernel();
Expand Down
Loading