Skip to content

Commit f789860

Browse files
authored
fix(serializer): exception message to not expose resource FQCN (#7156)
1 parent 937443d commit f789860

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Serializer/AbstractItemNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ public function denormalize(mixed $data, string $class, ?string $format = null,
227227
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
228228
}
229229

230-
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" resource "string" (IRI), "%s" given.', $resourceClass, \gettype($data)), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
230+
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
231231
} catch (InvalidArgumentException $e) {
232232
if (!isset($context['not_normalizable_value_exceptions'])) {
233233
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $data), $e->getCode(), $e);
234234
}
235235

236-
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The type of the "%s" resource "string" (IRI), "%s" given.', $resourceClass, \gettype($data)), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
236+
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Invalid IRI "%s".', $data), $data, [$resourceClass], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
237237
}
238238
}
239239

@@ -591,7 +591,7 @@ protected function denormalizeRelation(string $attributeName, ApiProperty $prope
591591
throw new UnexpectedValueException(\sprintf('Invalid IRI "%s".', $value), $e->getCode(), $e);
592592
}
593593

594-
throw NotNormalizableValueException::createForUnexpectedDataType($e->getMessage(), $value, [$className], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
594+
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('Invalid IRI "%s".', $value), $value, [$className], $context['deserialization_path'] ?? null, true, $e->getCode(), $e);
595595
}
596596
}
597597

src/Serializer/Tests/AbstractItemNormalizerTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Metadata\ApiProperty;
1717
use ApiPlatform\Metadata\ApiResource;
1818
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19+
use ApiPlatform\Metadata\Exception\ItemNotFoundException;
1920
use ApiPlatform\Metadata\Get;
2021
use ApiPlatform\Metadata\GetCollection;
2122
use ApiPlatform\Metadata\IriConverterInterface;
@@ -910,11 +911,13 @@ public function testDeserializationPathForNotDenormalizableRelations(): void
910911
$this->assertCount(1, $errors); // @phpstan-ignore-line method.impossibleType (false positive)
911912
$this->assertInstanceOf(NotNormalizableValueException::class, $errors[0]);
912913
$this->assertSame('relatedDummies[0]', $errors[0]->getPath());
914+
$this->assertSame('Invalid IRI "wrong".', $errors[0]->getMessage());
913915
}
914916

915917
public function testDeserializationPathForNotDenormalizableResource(): void
916918
{
917919
$this->expectException(NotNormalizableValueException::class);
920+
$this->expectExceptionMessage('Invalid IRI "wrong IRI".');
918921

919922
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
920923

@@ -949,6 +952,44 @@ public function testDeserializationPathForNotDenormalizableResource(): void
949952
$normalizer->denormalize('wrong IRI', Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
950953
}
951954

955+
public function testDeserializationPathForNotFoundResource(): void
956+
{
957+
$this->expectException(NotNormalizableValueException::class);
958+
$this->expectExceptionMessage('Some item not found exception.');
959+
960+
$propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
961+
962+
$propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
963+
964+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
965+
$iriConverterProphecy->getResourceFromIri(Argument::cetera())->willThrow(new ItemNotFoundException('Some item not found exception.'));
966+
967+
$resourceClassResolverProphecy = $this->prophesize(ResourceClassResolverInterface::class);
968+
$resourceClassResolverProphecy->getResourceClass(null, Dummy::class)->willReturn(Dummy::class);
969+
$resourceClassResolverProphecy->isResourceClass(Dummy::class)->willReturn(true);
970+
971+
$propertyAccessorProphecy = $this->prophesize(PropertyAccessorInterface::class);
972+
973+
$serializerProphecy = $this->prophesize(SerializerInterface::class);
974+
$serializerProphecy->willImplement(DenormalizerInterface::class);
975+
976+
$normalizer = $this->getMockForAbstractClass(AbstractItemNormalizer::class, [
977+
$propertyNameCollectionFactoryProphecy->reveal(),
978+
$propertyMetadataFactoryProphecy->reveal(),
979+
$iriConverterProphecy->reveal(),
980+
$resourceClassResolverProphecy->reveal(),
981+
$propertyAccessorProphecy->reveal(),
982+
null,
983+
null,
984+
[],
985+
null,
986+
null,
987+
]);
988+
$normalizer->setSerializer($serializerProphecy->reveal());
989+
990+
$normalizer->denormalize('/some-iri', Dummy::class, null, ['not_normalizable_value_exceptions' => []]);
991+
}
992+
952993
public function testInnerDocumentNotAllowed(): void
953994
{
954995
$this->expectException(UnexpectedValueException::class);

0 commit comments

Comments
 (0)