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
4 changes: 2 additions & 2 deletions features/hydra/item_uri_template.feature
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ Feature: Exposing a collection of objects should use the specified operation to
"hydra:member":[
{
"@id":"/item_referenced_in_collection/a",
"@type":"CollectionReferencingItem",
"@type":"ItemReferencedInCollection",
"id":"a",
"name":"hello"
},
{
"@id":"/item_referenced_in_collection/b",
"@type":"CollectionReferencingItem",
"@type":"ItemReferencedInCollection",
"id":"b",
"name":"you"
}
Expand Down
12 changes: 10 additions & 2 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
Expand Down Expand Up @@ -70,7 +71,7 @@ final class ItemNormalizer extends AbstractItemNormalizer
'@vocab',
];

public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null)
public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, private readonly ContextBuilderInterface $contextBuilder, ?PropertyAccessorInterface $propertyAccessor = null, ?NameConverterInterface $nameConverter = null, ?ClassMetadataFactoryInterface $classMetadataFactory = null, array $defaultContext = [], ?ResourceAccessCheckerInterface $resourceAccessChecker = null, protected ?TagCollectorInterface $tagCollector = null, private ?OperationMetadataFactoryInterface $operationMetadataFactory = null)
{
parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, $classMetadataFactory, $defaultContext, $resourceMetadataCollectionFactory, $resourceAccessChecker, $tagCollector);
}
Expand Down Expand Up @@ -115,7 +116,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
$context['output']['iri'] = null;
}

if ($this->resourceClassResolver->isResourceClass($resourceClass)) {
if (isset($context['item_uri_template']) && $this->operationMetadataFactory) {
$context['output']['operation'] = $this->operationMetadataFactory->create($context['item_uri_template']);
} elseif ($this->resourceClassResolver->isResourceClass($resourceClass)) {
$context['output']['operation'] = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
}

Expand All @@ -141,6 +144,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
}

$operation = $context['operation'] ?? null;

if ($this->operationMetadataFactory && isset($context['item_uri_template']) && !$operation) {
$operation = $this->operationMetadataFactory->create($context['item_uri_template']);
}

if ($isResourceClass && !$operation) {
$operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
}
Expand Down
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/Resources/config/jsonld.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<argument>%api_platform.serializer.default_context%</argument>
<argument type="service" id="api_platform.security.resource_access_checker" on-invalid="ignore" />
<argument type="service" id="api_platform.http_cache.tag_collector" on-invalid="ignore" />
<argument type="service" id="api_platform.metadata.operation.metadata_factory" on-invalid="ignore" />

<!-- Run before serializer.normalizer.json_serializable -->
<tag name="serializer.normalizer" priority="-890" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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 $recipeCategory = 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->recipeYield = '2 servings';

Check failure on line 69 in tests/Fixtures/TestBundle/ApiResource/ItemUriTemplateWithCollection/Recipe.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Access to an undefined property ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ItemUriTemplateWithCollection\Recipe::$recipeYield.
$recipe->recipeCategory = ['Lunch', 'Dinner'];

Check failure on line 70 in tests/Fixtures/TestBundle/ApiResource/ItemUriTemplateWithCollection/Recipe.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Property ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\ItemUriTemplateWithCollection\Recipe::$recipeCategory (string|null) does not accept array<int, string>.
$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;
}
}
Loading
Loading