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
9 changes: 9 additions & 0 deletions features/jsonld/disable_id_generation.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: Disable Id generation on anonymous resource collections

@!mongodb
@createSchema
Scenario: Get embed collection without ids
When I add "Accept" header equal to "application/ld+json"
And I send a "GET" request to "/disable_id_generation_collection"
Then the response status code should be 200
Then the JSON node "disableIdGenerationItems[0].@id" should not exist
3 changes: 2 additions & 1 deletion src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ protected function getAttributeValue(object $object, string $attribute, string $
}

if ('array' === $type->getBuiltinType()) {
$childContext = $this->createChildContext($this->createOperationContext($context, null), $attribute, $format);
$childContext = $this->createChildContext($context, $attribute, $format);
$childContext['output']['gen_id'] = $propertyMetadata->getGenId() ?? true;

$attributeValue = $this->propertyAccessor->getValue($object, $attribute);

Expand Down
46 changes: 46 additions & 0 deletions tests/Fixtures/TestBundle/Entity/DisableIdGeneration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Entity;

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

#[Get('disable_id_generation_collection', provider: [DisableIdGeneration::class, 'provide'])]
class DisableIdGeneration
{
#[ApiProperty(identifier: true)]
public int $id;

/**
* @var array<DisableIdGenerationItem>
*/
#[ApiProperty(genId: false)]
public array $disableIdGenerationItems;

public static function provide(Operation $operation, array $uriVariables = [], array $context = []): self
{
$a = new self();
$a->disableIdGenerationItems = [new DisableIdGenerationItem('test'), new DisableIdGenerationItem('test2')];

return $a;
}
}

class DisableIdGenerationItem
{
public function __construct(public string $title)
{
}
}