Skip to content

Commit

Permalink
fix(serializer): allow usage of genId property for collection (#5870)
Browse files Browse the repository at this point in the history
Co-authored-by: Gaëtan Petit <[email protected]>
  • Loading branch information
soyuka and gaetan-petit authored Oct 6, 2023
1 parent ce0bb9e commit c76d9b0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
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 <[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\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)
{
}
}

0 comments on commit c76d9b0

Please sign in to comment.