Skip to content

Commit

Permalink
fix(serializer): don't force resource class on relation (#5576)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Apr 28, 2023
1 parent 7f22569 commit 1281b0f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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');
}
Expand Down
4 changes: 3 additions & 1 deletion tests/Serializer/AbstractItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -577,6 +578,7 @@ public function testNormalizeReadableLinks(): void
];
$this->assertSame($expected, $normalizer->normalize($dummy, null, [
'resources' => [],
'force_resource_class' => Dummy::class,
]));
}

Expand Down

0 comments on commit 1281b0f

Please sign in to comment.