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
13 changes: 13 additions & 0 deletions features/hydra/item_uri_template.feature
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,16 @@ Feature: Exposing a collection of objects should use the specified operation to
]
}
"""

Scenario: Create an object with an itemUriTemplate should generate the IRI according to the specified itemUriTemplate
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/issue5662/books/a/reviews" with body:
"""
{
"body": "Good book"
}
"""
Then the response status code should be 201
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON node "@id" should be equal to "/issue5662/books/a/reviews/0"
2 changes: 1 addition & 1 deletion src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ protected function getAttributeValue(object $object, string $attribute, string $
&& $this->resourceClassResolver->isResourceClass($className)
) {
$childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
unset($childContext['iri'], $childContext['uri_variables']);
unset($childContext['iri'], $childContext['uri_variables'], $childContext['item_uri_template']);

if ('jsonld' === $format && $uriTemplate = $propertyMetadata->getUriTemplate()) {
$operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(
Expand Down
20 changes: 19 additions & 1 deletion tests/Fixtures/TestBundle/Entity/Issue5662/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\CreateProvider;

#[GetCollection(
uriTemplate: '/issue5662/admin/reviews{._format}',
Expand All @@ -43,12 +45,28 @@
'id' => new Link(fromClass: Review::class),
]
)]
#[Post(
itemUriTemplate: '/issue5662/books/{bookId}/reviews/{id}{._format}',
uriTemplate: '/issue5662/books/{id}/reviews{._format}',
uriVariables: [
'id' => new Link(toProperty: 'book', fromClass: Book::class),
],
provider: CreateProvider::class,
processor: [Review::class, 'process']
)]
class Review
{
public function __construct(public Book $book, public int $id, public string $body)
public function __construct(public ?Book $book = null, public ?int $id = null, public ?string $body = null)
{
}

public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
{
$data->id = 0;

return $data;
}

public static function getData(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
return [
Expand Down