Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1281b0f

Browse files
authored
fix(serializer): don't force resource class on relation (#5576)
1 parent 7f22569 commit 1281b0f

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

src/Serializer/AbstractItemNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
2626
use ApiPlatform\Metadata\Util\ClassInfoTrait;
2727
use ApiPlatform\Symfony\Security\ResourceAccessCheckerInterface;
28-
use ApiPlatform\Util\ClassInfoTrait;
2928
use ApiPlatform\Util\CloneTrait;
3029
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
3130
use Symfony\Component\PropertyAccess\PropertyAccess;
@@ -43,7 +42,6 @@
4342
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
4443
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
4544
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
46-
use Symfony\Component\Serializer\Serializer;
4745

4846
/**
4947
* Base item normalizer.
@@ -707,7 +705,9 @@ protected function normalizeRelation(ApiProperty $propertyMetadata, ?object $rel
707705
throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class));
708706
}
709707

710-
$normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $context);
708+
$relatedContext = $context;
709+
unset($relatedContext['force_resource_class']);
710+
$normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $relatedContext);
711711
if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) {
712712
throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null');
713713
}

tests/Serializer/AbstractItemNormalizerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,8 @@ public function testNormalizeReadableLinks(): void
551551
$relatedDummyChildContext = Argument::allOf(
552552
Argument::type('array'),
553553
Argument::withEntry('resource_class', RelatedDummy::class),
554-
Argument::not(Argument::withKey('iri'))
554+
Argument::not(Argument::withKey('iri')),
555+
Argument::not(Argument::withKey('force_resource_class'))
555556
);
556557
$serializerProphecy->normalize($relatedDummy, null, $relatedDummyChildContext)->willReturn(['foo' => 'hello']);
557558
$serializerProphecy->normalize(['foo' => 'hello'], null, Argument::type('array'))->willReturn(['foo' => 'hello']);
@@ -577,6 +578,7 @@ public function testNormalizeReadableLinks(): void
577578
];
578579
$this->assertSame($expected, $normalizer->normalize($dummy, null, [
579580
'resources' => [],
581+
'force_resource_class' => Dummy::class,
580582
]));
581583
}
582584

0 commit comments

Comments
 (0)