Skip to content

Commit ec00f0a

Browse files
committed
Feature : Catch wrong identifier type
Change catch and message Fix : Changed message Change InvalidArgument in InvalidIdentifier Adding test
1 parent 2ef093b commit ec00f0a

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/Bridge/Doctrine/Common/Util/IdentifierManagerTrait.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace ApiPlatform\Core\Bridge\Doctrine\Common\Util;
1515

16+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
1617
use ApiPlatform\Core\Exception\PropertyNotFoundException;
1718
use Doctrine\Common\Persistence\ObjectManager;
19+
use Doctrine\DBAL\Types\ConversionException;
1820
use Doctrine\DBAL\Types\Type as DBALType;
1921
use Doctrine\ODM\MongoDB\DocumentManager;
2022
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
@@ -34,6 +36,7 @@ trait IdentifierManagerTrait
3436
* @param int|string $id
3537
*
3638
* @throws PropertyNotFoundException
39+
* @throws InvalidIdentifierException
3740
*/
3841
private function normalizeIdentifiers($id, ObjectManager $manager, string $resourceClass): array
3942
{
@@ -76,11 +79,15 @@ private function normalizeIdentifiers($id, ObjectManager $manager, string $resou
7679

7780
$doctrineTypeName = $doctrineClassMetadata->getTypeOfField($propertyName);
7881

79-
if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) {
80-
$identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform);
81-
}
82-
if ($isOdm && null !== $doctrineTypeName && MongoDbType::hasType($doctrineTypeName)) {
83-
$identifier = MongoDbType::getType($doctrineTypeName)->convertToPHPValue($identifier);
82+
try {
83+
if ($isOrm && null !== $doctrineTypeName && DBALType::hasType($doctrineTypeName)) {
84+
$identifier = DBALType::getType($doctrineTypeName)->convertToPHPValue($identifier, $platform);
85+
}
86+
if ($isOdm && null !== $doctrineTypeName && MongoDbType::hasType($doctrineTypeName)) {
87+
$identifier = MongoDbType::getType($doctrineTypeName)->convertToPHPValue($identifier);
88+
}
89+
} catch (ConversionException $e) {
90+
throw new InvalidIdentifierException(sprintf('Invalid value "%s" provided for an identifier.', $propertyName), $e->getCode(), $e);
8491
}
8592

8693
$identifiers[$propertyName] = $identifier;

tests/Bridge/Doctrine/Common/Util/IdentifierManagerTraitTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace ApiPlatform\Core\Tests\Bridge\Doctrine\Common\Util;
1515

1616
use ApiPlatform\Core\Bridge\Doctrine\Common\Util\IdentifierManagerTrait;
17+
use ApiPlatform\Core\Exception\InvalidIdentifierException;
1718
use ApiPlatform\Core\Exception\PropertyNotFoundException;
1819
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
1920
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
@@ -31,6 +32,7 @@
3132
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
3233
use Doctrine\ORM\EntityManagerInterface;
3334
use PHPUnit\Framework\TestCase;
35+
use Ramsey\Uuid\Doctrine\UuidType;
3436

3537
class IdentifierManagerTraitTest extends TestCase
3638
{
@@ -204,4 +206,28 @@ private function getDocumentManager(string $resourceClass, array $identifierFiel
204206

205207
return $managerProphecy->reveal();
206208
}
209+
210+
/**
211+
* @group legacy
212+
*/
213+
public function testInvalidIdentifierConversion()
214+
{
215+
DBALType::addType('uuid', UuidType::class);
216+
217+
$this->expectException(InvalidIdentifierException::class);
218+
$this->expectExceptionMessage('Invalid value "ida" provided for an identifier.');
219+
220+
[$propertyNameCollectionFactory, $propertyMetadataFactory] = $this->getMetadataFactories(Dummy::class, [
221+
'ida',
222+
]);
223+
$objectManager = $this->getEntityManager(Dummy::class, [
224+
'ida' => [
225+
'type' => 'uuid',
226+
],
227+
]);
228+
229+
$identifierManager = $this->getIdentifierManagerTraitImpl($propertyNameCollectionFactory, $propertyMetadataFactory);
230+
231+
$identifierManager->normalizeIdentifiers('notanuuid', $objectManager, Dummy::class);
232+
}
207233
}

0 commit comments

Comments
 (0)