Skip to content

Commit f46c0a3

Browse files
authored
fix(jsonld): reset gen_id configuration (#7264)
1 parent 8e68d72 commit f46c0a3

File tree

5 files changed

+103
-6
lines changed

5 files changed

+103
-6
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
119119
$metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
120120
}
121121

122-
// maybe not needed anymore
123-
if (isset($context['operation']) && $previousResourceClass !== $resourceClass) {
124-
unset($context['operation'], $context['operation_name']);
122+
// Special case: non-resource got serialized and contains a resource therefore we need to reset part of the context
123+
if ($previousResourceClass !== $resourceClass) {
124+
unset($context['operation'], $context['operation_name'], $context['output']);
125125
}
126126

127127
if (true === ($context['output']['gen_id'] ?? true) && true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_PATH, $context['operation'] ?? null, $context)) {
@@ -136,9 +136,12 @@ public function normalize(mixed $object, ?string $format = null, array $context
136136
return $data;
137137
}
138138

139-
if (!isset($metadata['@type']) && $isResourceClass) {
140-
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
139+
$operation = $context['operation'] ?? null;
140+
if ($isResourceClass && !$operation) {
141+
$operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
142+
}
141143

144+
if (!isset($metadata['@type']) && $operation) {
142145
$types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
143146
if (null === $types) {
144147
$types = [$operation->getShortName()];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse;
15+
16+
use ApiPlatform\Metadata\ApiProperty;
17+
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Get;
19+
use ApiPlatform\Metadata\Operation;
20+
21+
#[ApiResource(operations: [new Get(uriTemplate: '/levelfirst/{id}', provider: [self::class, 'provider'])])]
22+
class LevelFirst
23+
{
24+
/**
25+
* @param list<LevelSecond> $levelSecond
26+
*/
27+
public function __construct(public string $id, #[ApiProperty(genId: false)] public array $levelSecond)
28+
{
29+
}
30+
31+
public static function provider(Operation $operation, array $uriVariables = [], array $context = []): self
32+
{
33+
return new self($uriVariables['id'], [new LevelSecond(new LevelThird('3', 'L3 Name'))]);
34+
}
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse;
15+
16+
class LevelSecond
17+
{
18+
public function __construct(public LevelThird $levelThird)
19+
{
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
19+
#[ApiResource(operations: [new Get(uriTemplate: '/levelthird/{id}')])]
20+
class LevelThird
21+
{
22+
public function __construct(public string $id, public string $name)
23+
{
24+
}
25+
}

tests/Functional/JsonLdTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
1717
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\AggregateRating;
1818
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\GenIdFalse;
19+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelFirst;
20+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelThird;
1921
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6810\JsonLdContextOutput;
2022
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
2123
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo;
@@ -34,7 +36,7 @@ class JsonLdTest extends ApiTestCase
3436
*/
3537
public static function getResources(): array
3638
{
37-
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class];
39+
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class];
3840
}
3941

4042
/**
@@ -81,6 +83,17 @@ public function testGenIdFalseOnResource(): void
8183
$this->assertArrayNotHasKey('@id', $r->toArray()['aggregateRating']);
8284
}
8385

86+
public function testGenIdFalseOnNestedResource(): void
87+
{
88+
$r = self::createClient()->request(
89+
'GET',
90+
'/levelfirst/1',
91+
);
92+
$res = $r->toArray();
93+
$this->assertArrayNotHasKey('@id', $res['levelSecond']);
94+
$this->assertArrayHasKey('@id', $res['levelSecond'][0]['levelThird']);
95+
}
96+
8497
public function testShouldIgnoreProperty(): void
8598
{
8699
$r = self::createClient()->request(

0 commit comments

Comments
 (0)