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
3 changes: 2 additions & 1 deletion src/JsonLd/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public function normalize(mixed $object, ?string $format = null, array $context
unset($context['operation'], $context['operation_name']);
}

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)) {
$operation = $context['operation'] ?? null;
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)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is probably not a necessary patch but it's probably better then the previous code

$context['iri'] = $iri;
$metadata['@id'] = $iri;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/OperationContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function createOperationContext(array $context, ?string $resourceClass
$context['root_operation_name'] = $context['operation_name'] ?? $context['graphql_operation_name'];
}

unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class']);
unset($context['iri'], $context['uri_variables'], $context['item_uri_template'], $context['force_resource_class'], $context['output']['gen_id']);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably reset the gen_id context between child context generation


// At some point we should merge the jsonld context here, there's a TODO to simplify this somewhere else
if ($propertyMetadata) {
Expand Down
32 changes: 32 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/GenIdFalse/LevelFirst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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
{
public function __construct(public string $id, #[ApiProperty(genId: false)] public LevelSecond $levelSecond)
{
}

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

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\NotExposed;

#[NotExposed()]
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 <[email protected]>
*
* 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)
{
}
}
18 changes: 17 additions & 1 deletion tests/Functional/JsonLdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
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\LevelSecond;
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 +37,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, LevelSecond::class, LevelThird::class];
}

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

public function testGenIdFalseOnNestedResource(): void
{
$r = self::createClient()->request(
'GET',
'/levelfirst/1',
);
$this->assertJsonContains([
'levelSecond' => [
'levelThird' => '/levelthird/3',
],
]);
}

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