Skip to content

Commit 3b42c9f

Browse files
authored
fix: deserialization path for not denormalizable relations collected errors (#6537)
1 parent 41deeb4 commit 3b42c9f

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/Serializer/AbstractItemNormalizer.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
use ApiPlatform\Api\IriConverterInterface as LegacyIriConverterInterface;
1717
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18-
use ApiPlatform\Exception\InvalidArgumentException;
18+
use ApiPlatform\Exception\InvalidArgumentException as LegacyInvalidArgumentException;
1919
use ApiPlatform\Exception\ItemNotFoundException;
2020
use ApiPlatform\Metadata\ApiProperty;
2121
use ApiPlatform\Metadata\CollectionOperationInterface;
22+
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
2223
use ApiPlatform\Metadata\IriConverterInterface;
2324
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2425
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
@@ -244,7 +245,7 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
244245
return $this->iriConverter->getResourceFromIri($data, $context + ['fetch_data' => true]);
245246
} catch (ItemNotFoundException $e) {
246247
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
247-
} catch (InvalidArgumentException $e) {
248+
} catch (LegacyInvalidArgumentException|InvalidArgumentException $e) {
248249
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e);
249250
}
250251
}
@@ -541,9 +542,14 @@ protected function denormalizeCollection(string $attribute, ApiProperty $propert
541542
$childContext = $this->createChildContext($this->createOperationContext($context, $className), $attribute, $format);
542543
$collectionKeyTypes = $type->getCollectionKeyTypes();
543544
foreach ($value as $index => $obj) {
545+
$currentChildContext = $childContext;
546+
if (isset($childContext['deserialization_path'])) {
547+
$currentChildContext['deserialization_path'] = "{$childContext['deserialization_path']}[{$index}]";
548+
}
549+
544550
// no typehint provided on collection key
545551
if (!$collectionKeyTypes) {
546-
$values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $childContext);
552+
$values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $currentChildContext);
547553
continue;
548554
}
549555

@@ -554,7 +560,7 @@ protected function denormalizeCollection(string $attribute, ApiProperty $propert
554560
continue;
555561
}
556562

557-
$values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $childContext);
563+
$values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $currentChildContext);
558564
continue 2;
559565
}
560566
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the key "%s" must be "%s", "%s" given.', $index, $collectionKeyTypes[0]->getBuiltinType(), \gettype($index)), $index, [$collectionKeyTypes[0]->getBuiltinType()], ($context['deserialization_path'] ?? false) ? \sprintf('key(%s)', $context['deserialization_path']) : null, true);
@@ -590,7 +596,7 @@ protected function denormalizeRelation(string $attributeName, ApiProperty $prope
590596
);
591597

592598
return null;
593-
} catch (InvalidArgumentException $e) {
599+
} catch (LegacyInvalidArgumentException|InvalidArgumentException $e) {
594600
if (!isset($context['not_normalizable_value_exceptions'])) {
595601
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $value), $e->getCode(), $e);
596602
}

src/Serializer/Tests/AbstractItemNormalizerTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Metadata\ApiProperty;
1717
use ApiPlatform\Metadata\ApiResource;
18+
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
1819
use ApiPlatform\Metadata\Get;
1920
use ApiPlatform\Metadata\GetCollection;
2021
use ApiPlatform\Metadata\IriConverterInterface;
@@ -1034,6 +1035,56 @@ public function testBadRelationTypeWithExceptionToValidationErrors(): void
10341035
$this->assertNull($actual->relatedDummy);
10351036
}
10361037

1038+
public function testDeserializationPathForNotDenormalizableRelations(): void
1039+
{
1040+
$data = [
1041+
'relatedDummies' => ['wrong'],
1042+
];
1043+
1044+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
1045+
$propertyNameCollectionFactoryProphecy->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['relatedDummies']));
1046+
1047+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
1048+
$propertyMetadataFactoryProphecy->create(Dummy::class, 'relatedDummies', [])->willReturn(
1049+
(new ApiProperty())->withBuiltinTypes([new Type(Type::BUILTIN_TYPE_OBJECT, false, null, true, null, new Type(Type::BUILTIN_TYPE_OBJECT, false, RelatedDummy::class))])->withReadable(false)->withWritable(true)->withReadableLink(false)->withWritableLink(true)
1050+
);
1051+
1052+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
1053+
$iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new InvalidArgumentException('Invalid IRI'));
1054+
1055+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
1056+
$resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
1057+
$resourceClassResolverProphecy->getResourceClass(null, RelatedDummy::class)->willReturn(RelatedDummy::class);
1058+
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
1059+
$resourceClassResolverProphecy->isResourceClass(RelatedDummy::class)->willReturn(true);
1060+
1061+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
1062+
1063+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
1064+
$serializerProphecy->willImplement(DenormalizerInterface::class);
1065+
1066+
$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
1067+
$propertyNameCollectionFactoryProphecy->reveal(),
1068+
$propertyMetadataFactoryProphecy->reveal(),
1069+
$iriConverterProphecy->reveal(),
1070+
$resourceClassResolverProphecy->reveal(),
1071+
$propertyAccessorProphecy->reveal(),
1072+
null,
1073+
null,
1074+
[],
1075+
null,
1076+
null,
1077+
]);
1078+
$normalizer->setSerializer($serializerProphecy->reveal());
1079+
1080+
$errors = [];
1081+
$actual = $normalizer->denormalize($data, Dummy::class, null, ['not_normalizable_value_exceptions' => &$errors]);
1082+
$this->assertEmpty($actual->relatedDummies);
1083+
$this->assertCount(1, $errors); // @phpstan-ignore-line method.impossibleType (false positive)
1084+
$this->assertInstanceOf(NotNormalizableValueException::class, $errors[0]);
1085+
$this->assertSame('relatedDummies[0]', $errors[0]->getPath());
1086+
}
1087+
10371088
public function testInnerDocumentNotAllowed(): void
10381089
{
10391090
$this->expectException(UnexpectedValueException::class);

0 commit comments

Comments
 (0)