From a8eb9a6c965947a9804667c537d86964c07944c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Petit?= Date: Fri, 29 Sep 2023 22:07:34 +0200 Subject: [PATCH] fix(serializer): allow usage of genId property for collection --- features/jsonld/disable_id_generation.feature | 9 ++++ src/Serializer/AbstractItemNormalizer.php | 3 +- .../TestBundle/Entity/DisableIdGeneration.php | 46 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 features/jsonld/disable_id_generation.feature create mode 100644 tests/Fixtures/TestBundle/Entity/DisableIdGeneration.php diff --git a/features/jsonld/disable_id_generation.feature b/features/jsonld/disable_id_generation.feature new file mode 100644 index 00000000000..0e396ebe7ad --- /dev/null +++ b/features/jsonld/disable_id_generation.feature @@ -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 diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index 96119676fe0..fb25fa780b9 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -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); diff --git a/tests/Fixtures/TestBundle/Entity/DisableIdGeneration.php b/tests/Fixtures/TestBundle/Entity/DisableIdGeneration.php new file mode 100644 index 00000000000..5c1eb02c4df --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/DisableIdGeneration.php @@ -0,0 +1,46 @@ + + * + * 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 + */ + #[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) + { + } +}