From 1281b0f491f5656a7554265858460bb768329ed4 Mon Sep 17 00:00:00 2001 From: Antoine Bluchet Date: Fri, 28 Apr 2023 10:56:07 +0200 Subject: [PATCH] fix(serializer): don't force resource class on relation (#5576) --- src/Serializer/AbstractItemNormalizer.php | 6 +++--- tests/Serializer/AbstractItemNormalizerTest.php | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Serializer/AbstractItemNormalizer.php b/src/Serializer/AbstractItemNormalizer.php index a0cbecb1b1b..db476438554 100644 --- a/src/Serializer/AbstractItemNormalizer.php +++ b/src/Serializer/AbstractItemNormalizer.php @@ -25,7 +25,6 @@ use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\Util\ClassInfoTrait; use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface; -use ApiPlatform\Util\ClassInfoTrait; use ApiPlatform\Util\CloneTrait; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; use Symfony\Component\PropertyAccess\PropertyAccess; @@ -43,7 +42,6 @@ use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Serializer; /** * Base item normalizer. @@ -707,7 +705,9 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class)); } - $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $context); + $relatedContext = $context; + unset($relatedContext['force_resource_class']); + $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $relatedContext); if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) { throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null'); } diff --git a/tests/Serializer/AbstractItemNormalizerTest.php b/tests/Serializer/AbstractItemNormalizerTest.php index d1c59d4599b..ca4aeccac38 100644 --- a/tests/Serializer/AbstractItemNormalizerTest.php +++ b/tests/Serializer/AbstractItemNormalizerTest.php @@ -551,7 +551,8 @@ public function testNormalizeReadableLinks(): void $relatedDummyChildContext = Argument::allOf( Argument::type('array'), Argument::withEntry('resource_class', RelatedDummy::class), - Argument::not(Argument::withKey('iri')) + Argument::not(Argument::withKey('iri')), + Argument::not(Argument::withKey('force_resource_class')) ); $serializerProphecy->normalize($relatedDummy, null, $relatedDummyChildContext)->willReturn(['foo' => 'hello']); $serializerProphecy->normalize(['foo' => 'hello'], null, Argument::type('array'))->willReturn(['foo' => 'hello']); @@ -577,6 +578,7 @@ public function testNormalizeReadableLinks(): void ]; $this->assertSame($expected, $normalizer->normalize($dummy, null, [ 'resources' => [], + 'force_resource_class' => Dummy::class, ])); }