Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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\ItemUriTemplateWithCollection;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Recipe as EntityRecipe;

#[Get(
uriTemplate: '/item_uri_template_recipes/{id}{._format}',
shortName: 'ItemRecipe',
uriVariables: ['id'],
provider: [self::class, 'provide'],
openapi: false
)]
#[Get(
uriTemplate: '/item_uri_template_recipes_state_option/{id}{._format}',
shortName: 'ItemRecipe',
uriVariables: ['id'],
openapi: false,
stateOptions: new Options(entityClass: EntityRecipe::class)
)]
class Recipe
{
public ?string $id;
public ?string $name = null;

public ?string $description = null;

public ?string $author = null;

public ?array $recipeIngredient = [];

public ?string $recipeInstructions = null;

public ?string $prepTime = null;

public ?string $cookTime = null;

public ?string $totalTime = null;

public ?string $recipeCuisine = null;

public ?string $suitableForDiet = null;

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$recipe = new self();
$recipe->id = '1';
$recipe->name = 'Dummy Recipe';
$recipe->description = 'A simple recipe for testing purposes.';
$recipe->prepTime = 'PT15M';
$recipe->cookTime = 'PT30M';
$recipe->totalTime = 'PT45M';
$recipe->recipeIngredient = ['Ingredient 1', 'Ingredient 2'];
$recipe->recipeInstructions = 'Do these things.';

return $recipe;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\ItemUriTemplateWithCollection;

use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Recipe as EntityRecipe;

#[GetCollection(
uriTemplate: '/item_uri_template_recipes{._format}',
provider: [self::class, 'provide'],
openapi: false,
shortName: 'CollectionRecipe',
itemUriTemplate: '/item_uri_template_recipes/{id}{._format}',
normalizationContext: ['hydra_prefix' => false],
)]
#[GetCollection(
uriTemplate: '/item_uri_template_recipes_state_option{._format}',
openapi: false,
shortName: 'CollectionRecipe',
itemUriTemplate: '/item_uri_template_recipes_state_option/{id}{._format}',
stateOptions: new Options(entityClass: EntityRecipe::class),
normalizationContext: ['hydra_prefix' => false],
)]
class RecipeCollection
{
public ?string $id;
public ?string $name = null;

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$recipe = new self();
$recipe->id = '1';
$recipe->name = 'Dummy Recipe';

$recipe2 = new self();
$recipe2->id = '2';
$recipe2->name = 'Dummy Recipe 2';

return [$recipe, $recipe2];
}
}
63 changes: 63 additions & 0 deletions tests/Fixtures/TestBundle/Entity/Recipe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class Recipe
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $name = null;

#[ORM\Column(type: 'text', nullable: true)]
public ?string $description = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $author = null;

#[ORM\Column(type: 'json', nullable: true)]
public ?array $recipeIngredient = [];

#[ORM\Column(type: 'text', nullable: true)]
public ?string $recipeInstructions = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $prepTime = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $cookTime = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $totalTime = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $recipeCategory = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $recipeCuisine = null;

#[ORM\Column(type: 'string', nullable: true)]
public ?string $suitableForDiet = null;

public function getId(): ?int
{
return $this->id;
}
}
97 changes: 95 additions & 2 deletions tests/Functional/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
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\Fixtures\TestBundle\Entity\Recipe as EntityRecipe;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
Expand All @@ -39,7 +42,20 @@ class JsonLdTest extends ApiTestCase
*/
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,
RecipeCollection::class,
];
}

/**
Expand Down Expand Up @@ -129,6 +145,83 @@ public function testIssue7298(): void
]);
}

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',
],
],
]);
}

public function testItemUriTemplateWithStateOption(): void
{
$container = static::getContainer();
$registry = $container->get('doctrine');
$manager = $registry->getManager();
for ($i = 0; $i < 10; ++$i) {
$recipe = new EntityRecipe();
$recipe->name = "Recipe $i";
$recipe->description = "Description of recipe $i";
$recipe->author = "Author $i";
$recipe->recipeIngredient = [
"Ingredient 1 for recipe $i",
"Ingredient 2 for recipe $i",
];
$recipe->recipeInstructions = "Instructions for recipe $i";
$recipe->prepTime = '10 minutes';
$recipe->cookTime = '20 minutes';
$recipe->totalTime = '30 minutes';
$recipe->recipeCategory = "Category $i";
$recipe->recipeCuisine = "Cuisine $i";
$recipe->suitableForDiet = "Diet $i";

$manager->persist($recipe);
}
$manager->flush();

self::createClient()->request(
'GET',
'/item_uri_template_recipes_state_option',
);
$this->assertResponseIsSuccessful();

$this->assertJsonContains([
'member' => [
[
'@type' => 'Recipe',
'@id' => '/item_uri_template_recipes_state_option/1',
'name' => 'Recipe 0',
],
[
'@type' => 'Recipe',
'@id' => '/item_uri_template_recipes_state_option/2',
'name' => 'Recipe 1',
],
[
'@type' => 'Recipe',
'@id' => '/item_uri_template_recipes_state_option/3',
'name' => 'Recipe 2',
],
],
]);
}

protected function setUp(): void
{
self::bootKernel();
Expand All @@ -141,7 +234,7 @@ protected function setUp(): void
}

$classes = [];
foreach ([Foo::class, Bar::class] as $entityClass) {
foreach ([Foo::class, Bar::class, EntityRecipe::class] as $entityClass) {
$classes[] = $manager->getClassMetadata($entityClass);
}

Expand Down
Loading