Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c7012db

Browse filesBrowse files
committed
[DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers
1 parent e2a8117 commit c7012db
Copy full SHA for c7012db

File tree

4 files changed

+84
-0
lines changed
Filter options

4 files changed

+84
-0
lines changed
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Symfony\Component\Uid\Uuid;
15+
16+
class UserUuidNameDto
17+
{
18+
public function __construct(
19+
public ?Uuid $id,
20+
public ?string $fullName,
21+
public ?string $address,
22+
) {
23+
}
24+
}
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
use Symfony\Component\Uid\Uuid;
18+
19+
#[Entity]
20+
class UserUuidNameEntity
21+
{
22+
public function __construct(
23+
#[Id, Column]
24+
public ?Uuid $id = null,
25+
#[Column(unique: true)]
26+
public ?string $fullName = null,
27+
) {
28+
}
29+
}

‎src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@
4343
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeIntIdEntity;
4444
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeObjectNoToStringIdEntity;
4545
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateEmployeeProfile;
46+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameDto;
47+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameEntity;
4648
use Symfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
4749
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
4850
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
51+
use Symfony\Component\Uid\Uuid;
4952
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
5053
use Symfony\Component\Validator\Exception\UnexpectedValueException;
5154
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -118,6 +121,7 @@ private function createSchema($em)
118121
$em->getClassMetadata(Employee::class),
119122
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
120123
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
124+
$em->getClassMetadata(UserUuidNameEntity::class),
121125
]);
122126
}
123127

@@ -1403,4 +1407,25 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()
14031407

14041408
$this->validator->validate($dto, $constraint);
14051409
}
1410+
1411+
public function testUuidIdentifierWithSameValueDifferentInstanceDoesNotCauseViolation()
1412+
{
1413+
$uuidString = 'ec562e21-1fc8-4e55-8de7-a42389ac75c5';
1414+
$existingPerson = new UserUuidNameEntity(Uuid::fromString($uuidString), 'FooBar');
1415+
$this->em->persist($existingPerson);
1416+
$this->em->flush();
1417+
1418+
$dto = new UserUuidNameDto(Uuid::fromString($uuidString), 'FooBar', '');
1419+
1420+
$constraint = new UniqueEntity(
1421+
fields: ['fullName'],
1422+
entityClass: UserUuidNameEntity::class,
1423+
identifierFieldNames: ['id'],
1424+
em: self::EM_NAME,
1425+
);
1426+
1427+
$this->validator->validate($dto, $constraint);
1428+
1429+
$this->assertNoViolation();
1430+
}
14061431
}

‎src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public function validate(mixed $value, Constraint $constraint): void
197197

198198
foreach ($constraint->identifierFieldNames as $identifierFieldName) {
199199
$propertyValue = $this->getPropertyValue($entityClass, $identifierFieldName, current($result));
200+
if ($fieldValues[$identifierFieldName] instanceof \Stringable) {
201+
$fieldValues[$identifierFieldName] = (string) $fieldValues[$identifierFieldName];
202+
}
203+
if ($propertyValue instanceof \Stringable) {
204+
$propertyValue = (string) $propertyValue;
205+
}
200206
if ($fieldValues[$identifierFieldName] !== $propertyValue) {
201207
$entityMatched = false;
202208
break;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.