Skip to content

Commit ccf52c1

Browse files
fix: item_uri_template conflict with context on relation (#6015)
* fix: item_uri_template conflict with context on relation * test --------- Co-authored-by: soyuka <soyuka@users.noreply.github.com>
1 parent dcfd3c5 commit ccf52c1

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

features/hydra/item_uri_template.feature

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,16 @@ Feature: Exposing a collection of objects should use the specified operation to
220220
]
221221
}
222222
"""
223+
224+
Scenario: Create an object with an itemUriTemplate should generate the IRI according to the specified itemUriTemplate
225+
When I add "Content-Type" header equal to "application/ld+json"
226+
And I send a "POST" request to "/issue5662/books/a/reviews" with body:
227+
"""
228+
{
229+
"body": "Good book"
230+
}
231+
"""
232+
Then the response status code should be 201
233+
And the response should be in JSON
234+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
235+
And the JSON node "@id" should be equal to "/issue5662/books/a/reviews/0"

src/Serializer/AbstractItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ protected function getAttributeValue(object $object, string $attribute, string $
678678
&& $this->resourceClassResolver->isResourceClass($className)
679679
) {
680680
$childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
681-
unset($childContext['iri'], $childContext['uri_variables']);
681+
unset($childContext['iri'], $childContext['uri_variables'], $childContext['item_uri_template']);
682682

683683
if ('jsonld' === $format && $uriTemplate = $propertyMetadata->getUriTemplate()) {
684684
$operation = $this->resourceMetadataCollectionFactory->create($className)->getOperation(

tests/Fixtures/TestBundle/Entity/Issue5662/Review.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use ApiPlatform\Metadata\GetCollection;
1818
use ApiPlatform\Metadata\Link;
1919
use ApiPlatform\Metadata\Operation;
20+
use ApiPlatform\Metadata\Post;
21+
use ApiPlatform\State\CreateProvider;
2022

2123
#[GetCollection(
2224
uriTemplate: '/issue5662/admin/reviews{._format}',
@@ -43,12 +45,28 @@
4345
'id' => new Link(fromClass: Review::class),
4446
]
4547
)]
48+
#[Post(
49+
itemUriTemplate: '/issue5662/books/{bookId}/reviews/{id}{._format}',
50+
uriTemplate: '/issue5662/books/{id}/reviews{._format}',
51+
uriVariables: [
52+
'id' => new Link(toProperty: 'book', fromClass: Book::class),
53+
],
54+
provider: CreateProvider::class,
55+
processor: [Review::class, 'process']
56+
)]
4657
class Review
4758
{
48-
public function __construct(public Book $book, public int $id, public string $body)
59+
public function __construct(public ?Book $book = null, public ?int $id = null, public ?string $body = null)
4960
{
5061
}
5162

63+
public static function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
64+
{
65+
$data->id = 0;
66+
67+
return $data;
68+
}
69+
5270
public static function getData(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
5371
{
5472
return [

0 commit comments

Comments
 (0)