Skip to content

Commit d153826

Browse files
committed
edit
1 parent 4e5741c commit d153826

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,12 @@ 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

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)) {
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)) {
129128
$context['iri'] = $iri;
130129
$metadata['@id'] = $iri;
131130
}
@@ -137,9 +136,12 @@ public function normalize(mixed $object, ?string $format = null, array $context
137136
return $data;
138137
}
139138

140-
if (!isset($metadata['@type']) && $isResourceClass) {
141-
$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+
}
142143

144+
if (!isset($metadata['@type']) && $operation) {
143145
$types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
144146
if (null === $types) {
145147
$types = [$operation->getShortName()];

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'], $context['output']['gen_id']);
37+
unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class']);
3838

3939
// At some point we should merge the jsonld context here, there's a TODO to simplify this somewhere else
4040
if ($propertyMetadata) {

tests/Fixtures/TestBundle/ApiResource/GenIdFalse/LevelFirst.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
#[ApiResource(operations: [new Get(uriTemplate: '/levelfirst/{id}', provider: [self::class, 'provider'])])]
2222
class LevelFirst
2323
{
24-
public function __construct(public string $id, #[ApiProperty(genId: false)] public LevelSecond $levelSecond)
24+
/**
25+
* @param list<LevelSecond> $levelSecond
26+
*/
27+
public function __construct(public string $id, #[ApiProperty(genId: false)] public array $levelSecond)
2528
{
2629
}
2730

2831
public static function provider(Operation $operation, array $uriVariables = [], array $context = []): self
2932
{
30-
return new self($uriVariables['id'], new LevelSecond(new LevelThird('3', 'L3 Name')));
33+
return new self($uriVariables['id'], [new LevelSecond(new LevelThird('3', 'L3 Name'))]);
3134
}
3235
}

tests/Fixtures/TestBundle/ApiResource/GenIdFalse/LevelSecond.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse;
1515

16-
use ApiPlatform\Metadata\NotExposed;
17-
18-
#[NotExposed()]
1916
class LevelSecond
2017
{
2118
public function __construct(public LevelThird $levelThird)

tests/Functional/JsonLdTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\AggregateRating;
1818
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\GenIdFalse;
1919
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelFirst;
20-
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelSecond;
2120
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelThird;
2221
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6810\JsonLdContextOutput;
2322
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
@@ -37,7 +36,7 @@ class JsonLdTest extends ApiTestCase
3736
*/
3837
public static function getResources(): array
3938
{
40-
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelSecond::class, LevelThird::class];
39+
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class];
4140
}
4241

4342
/**
@@ -90,11 +89,9 @@ public function testGenIdFalseOnNestedResource(): void
9089
'GET',
9190
'/levelfirst/1',
9291
);
93-
$this->assertJsonContains([
94-
'levelSecond' => [
95-
'levelThird' => '/levelthird/3',
96-
],
97-
]);
92+
$res = $r->toArray();
93+
$this->assertArrayNotHasKey('@id', $res['levelSecond']);
94+
$this->assertArrayHasKey('@id', $res['levelSecond'][0]['levelThird']);
9895
}
9996

10097
public function testShouldIgnoreProperty(): void

0 commit comments

Comments
 (0)