Skip to content

fix(serializer): retrieve only first uriVariable from operation #5788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
Expand Down Expand Up @@ -77,6 +77,13 @@ private function updateObjectToPopulate(array $data, array &$context): void
$context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getResourceFromIri((string) $data['id'], $context + ['fetch_data' => true]);
} catch (InvalidArgumentException) {
$operation = $this->resourceMetadataCollectionFactory->create($context['resource_class'])->getOperation();
if (
null !== ($context['uri_variables'] ?? null)
&& $operation instanceof HttpOperation
&& \count($operation->getUriVariables() ?? []) > 1
) {
throw new InvalidArgumentException('Cannot find object to populate, use JSON-LD or specify an IRI at path "id".');
}
$uriVariables = $this->getContextUriVariables($data, $operation, $context);
$iri = $this->iriConverter->getIriFromResource($context['resource_class'], UrlGeneratorInterface::ABS_PATH, $operation, ['uri_variables' => $uriVariables]);

Expand All @@ -86,18 +93,13 @@ private function updateObjectToPopulate(array $data, array &$context): void

private function getContextUriVariables(array $data, $operation, array $context): array
{
$uriVariables = $context['uri_variables'] ?? $data;

/** @var Link $uriVariable */
foreach ($operation->getUriVariables() as $uriVariable) {
if (isset($uriVariables[$uriVariable->getParameterName()])) {
continue;
}
$uriVariables = $context['uri_variables'] ?? [];

foreach ($uriVariable->getIdentifiers() as $identifier) {
if (isset($data[$identifier])) {
$uriVariables[$uriVariable->getParameterName()] = $data[$identifier];
}
$operationUriVariables = $operation->getUriVariables();
if ((null !== $uriVariable = array_shift($operationUriVariables)) && \count($uriVariable->getIdentifiers())) {
$identifier = $uriVariable->getIdentifiers()[0];
if (isset($data[$identifier])) {
$uriVariables[$uriVariable->getParameterName()] = $data[$identifier];
}
}

Expand Down
71 changes: 0 additions & 71 deletions tests/Serializer/ItemNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\ResourceClassResolverInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Property\PropertyNameCollection;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Serializer\ItemNormalizer;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -189,69 +181,6 @@ public function testDenormalizeWithIri(): void
$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['id' => '/dummies/12', 'name' => 'hello'], Dummy::class, null, $context));
}

public function testDenormalizeGuessingUriVariables(): void
Copy link
Member

@soyuka soyuka Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong and can not work in a real case scenario so we remove it

{
$context = ['resource_class' => Dummy::class, 'api_allow_update' => true, 'uri_variables' => [
'parent_resource' => '1',
'resource' => '1',
]];

$propertyNameCollection = new PropertyNameCollection(['name']);
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$propertyNameCollectionFactoryProphecy->create(Dummy::class, Argument::cetera())->willReturn($propertyNameCollection)->shouldBeCalled();

$propertyMetadata = (new ApiProperty())->withReadable(true)->withWritable(true);
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
$propertyMetadataFactoryProphecy->create(Dummy::class, 'name', Argument::cetera())->willReturn($propertyMetadata)->shouldBeCalled();

$uriVariables = [
'parent_resource' => new Link('parent_resource', identifiers: ['id']),
'resource' => new Link('resource', identifiers: ['id']),
'sub_resource' => new Link('sub_resource', identifiers: ['id']),
];
$resourceMetadataCollectionFactoryProphecy = $this->prophesize(ResourceMetadataCollectionFactoryInterface::class);
$resourceMetadataCollectionFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadataCollection(Dummy::class, [
(new ApiResource())->withShortName('Dummy')->withOperations(new Operations([
'sub_resource' => (new Get(uriVariables: $uriVariables))->withShortName('Dummy'),
])),
]));

$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
$resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->willImplement(DenormalizerInterface::class);

$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
$iriConverterProphecy->getResourceFromIri(Argument::is('12'), Argument::cetera())->willThrow(InvalidArgumentException::class);
$iriConverterProphecy
->getIriFromResource(
Dummy::class,
UrlGeneratorInterface::ABS_PATH,
Argument::type(Get::class),
Argument::withEntry('uri_variables', Argument::allOf(
Argument::withEntry('parent_resource', '1'),
Argument::withEntry('resource', '1'),
Argument::withEntry('sub_resource', '12')
))
)
->willReturn('parent_resource/1/resource/1/sub_resource/2')
->shouldBeCalledOnce();
$iriConverterProphecy->getResourceFromIri('parent_resource/1/resource/1/sub_resource/2', ['fetch_data' => true])->shouldBeCalledOnce();

$normalizer = new ItemNormalizer(
$propertyNameCollectionFactoryProphecy->reveal(),
$propertyMetadataFactoryProphecy->reveal(),
$iriConverterProphecy->reveal(),
$resourceClassResolverProphecy->reveal(),
resourceMetadataFactory: $resourceMetadataCollectionFactoryProphecy->reveal(),
);
$normalizer->setSerializer($serializerProphecy->reveal());

$this->assertInstanceOf(Dummy::class, $normalizer->denormalize(['id' => '12', 'name' => 'hello'], Dummy::class, null, $context));
}

public function testDenormalizeWithIdAndUpdateNotAllowed(): void
{
$this->expectException(NotNormalizableValueException::class);
Expand Down