Skip to content

Commit 454fe71

Browse files
committed
fix(jsonld): reset gen_id configuration
1 parent 23285ae commit 454fe71

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ public function normalize(mixed $object, ?string $format = null, array $context
124124
unset($context['operation'], $context['operation_name']);
125125
}
126126

127-
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)) {
127+
$operation = $context['operation'] ?? null;
128+
if (true === ($context['output']['gen_id'] ?? true) && true === ($context['force_iri_generation'] ?? true) && $iri = $this->iriConverter->getIriFromResource($object, $operation?->getUrlGenerationStrategy() ?? UrlGeneratorInterface::ABS_PATH, $operation, $context)) {
128129
$context['iri'] = $iri;
129130
$metadata['@id'] = $iri;
130131
}

src/Serializer/OperationContextTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function createOperationContext(array $context, ?string $resourceClass
3434
$context['root_operation_name'] = $context['operation_name'] ?? $context['graphql_operation_name'];
3535
}
3636

37-
unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class']);
37+
unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class'], $context['output']['gen_id']);
3838

3939
// At some point we should merge the jsonld context here, there's a TODO to simplify this somewhere else
4040
if ($propertyMetadata) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
public function __construct(public string $id, #[ApiProperty(genId: false)] public LevelSecond $levelSecond)
25+
{
26+
}
27+
28+
public static function provider(Operation $operation, array $uriVariables = [], array $context = []): self
29+
{
30+
return new self($uriVariables['id'], new LevelSecond(new LevelThird('3', 'L3 Name')));
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\NotExposed;
17+
18+
#[NotExposed()]
19+
class LevelSecond
20+
{
21+
public function __construct(public LevelThird $levelThird)
22+
{
23+
}
24+
}
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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
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\LevelSecond;
21+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelThird;
1922
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6810\JsonLdContextOutput;
2023
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
2124
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo;
@@ -34,7 +37,7 @@ class JsonLdTest extends ApiTestCase
3437
*/
3538
public static function getResources(): array
3639
{
37-
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class];
40+
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelSecond::class, LevelThird::class];
3841
}
3942

4043
/**
@@ -81,6 +84,19 @@ public function testGenIdFalseOnResource(): void
8184
$this->assertArrayNotHasKey('@id', $r->toArray()['aggregateRating']);
8285
}
8386

87+
public function testGenIdFalseOnNestedResource(): void
88+
{
89+
$r = self::createClient()->request(
90+
'GET',
91+
'/levelfirst/1',
92+
);
93+
$this->assertJsonContains([
94+
'levelSecond' => [
95+
'levelThird' => '/levelthird/3',
96+
],
97+
]);
98+
}
99+
84100
public function testShouldIgnoreProperty(): void
85101
{
86102
$r = self::createClient()->request(

0 commit comments

Comments
 (0)