Skip to content

Navigation Menu

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 e80f5d7

Browse filesBrowse files
committed
bug #60275 [DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers (GiuseppeArcuti, wkania)
This PR was merged into the 7.2 branch. Discussion ---------- [DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #58883 | License | MIT `@GiuseppeArcuti` , thanks for creating the test in [PR](#59126). More info in the [comment](#59126 (comment)). Commits ------- b0f012f [DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers 572ebe8 [DoctrineBridge] Fix UniqueEntity for non-integer identifiers
2 parents 310f769 + b0f012f commit e80f5d7
Copy full SHA for e80f5d7

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
@@ -41,9 +41,12 @@
4141
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeIntIdEntity;
4242
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeObjectNoToStringIdEntity;
4343
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateEmployeeProfile;
44+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameDto;
45+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameEntity;
4446
use Symfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
4547
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
4648
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
49+
use Symfony\Component\Uid\Uuid;
4750
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
4851
use Symfony\Component\Validator\Exception\UnexpectedValueException;
4952
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -116,6 +119,7 @@ private function createSchema($em)
116119
$em->getClassMetadata(Employee::class),
117120
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
118121
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
122+
$em->getClassMetadata(UserUuidNameEntity::class),
119123
]);
120124
}
121125

@@ -1401,4 +1405,25 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()
14011405

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

‎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.