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: 8 additions & 5 deletions src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
$metadata = $this->createJsonLdContext($this->contextBuilder, $object, $context);
}

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

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)) {
Expand All @@ -136,9 +136,12 @@ public function normalize(mixed $object, ?string $format = null, array $context
return $data;
}

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

if (!isset($metadata['@type']) && $operation) {
$types = $operation instanceof HttpOperation ? $operation->getTypes() : null;
if (null === $types) {
$types = [$operation->getShortName()];
Expand Down
35 changes: 35 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/GenIdFalse/LevelFirst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

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

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;

#[ApiResource(operations: [new Get(uriTemplate: '/levelfirst/{id}', provider: [self::class, 'provider'])])]
class LevelFirst
{
/**
* @param list<LevelSecond> $levelSecond
*/
public function __construct(public string $id, #[ApiProperty(genId: false)] public array $levelSecond)
{
}

public static function provider(Operation $operation, array $uriVariables = [], array $context = []): self
{
return new self($uriVariables['id'], [new LevelSecond(new LevelThird('3', 'L3 Name'))]);
}
}
21 changes: 21 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/GenIdFalse/LevelSecond.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

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

class LevelSecond
{
public function __construct(public LevelThird $levelThird)
{
}
}
25 changes: 25 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/GenIdFalse/LevelThird.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

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

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;

#[ApiResource(operations: [new Get(uriTemplate: '/levelthird/{id}')])]
class LevelThird
{
public function __construct(public string $id, public string $name)
{
}
}
15 changes: 14 additions & 1 deletion tests/Functional/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\AggregateRating;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\GenIdFalse;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelFirst;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\GenIdFalse\LevelThird;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6810\JsonLdContextOutput;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Bar;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6465\Foo;
Expand All @@ -34,7 +36,7 @@ class JsonLdTest extends ApiTestCase
*/
public static function getResources(): array
{
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class];
return [Foo::class, Bar::class, JsonLdContextOutput::class, GenIdFalse::class, AggregateRating::class, LevelFirst::class, LevelThird::class];
}

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

public function testGenIdFalseOnNestedResource(): void
{
$r = self::createClient()->request(
'GET',
'/levelfirst/1',
);
$res = $r->toArray();
$this->assertArrayNotHasKey('@id', $res['levelSecond']);
$this->assertArrayHasKey('@id', $res['levelSecond'][0]['levelThird']);
}

public function testShouldIgnoreProperty(): void
{
$r = self::createClient()->request(
Expand Down
Loading