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

[DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers #60275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions 24 src/Symfony/Bridge/Doctrine/Tests/Fixtures/UserUuidNameDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Symfony\Component\Uid\Uuid;

class UserUuidNameDto
{
public function __construct(
public ?Uuid $id,
public ?string $fullName,
public ?string $address,
) {
}
}
29 changes: 29 additions & 0 deletions 29 src/Symfony/Bridge/Doctrine/Tests/Fixtures/UserUuidNameEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Symfony\Component\Uid\Uuid;

#[Entity]
class UserUuidNameEntity
{
public function __construct(
#[Id, Column]
public ?Uuid $id = null,
#[Column(unique: true)]
public ?string $fullName = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeObjectNoToStringIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateEmployeeProfile;
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameDto;
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameEntity;
use Symfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
Expand Down Expand Up @@ -116,6 +119,7 @@ private function createSchema($em)
$em->getClassMetadata(Employee::class),
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
$em->getClassMetadata(UserUuidNameEntity::class),
]);
}

Expand Down Expand Up @@ -1401,4 +1405,25 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()

$this->validator->validate($dto, $constraint);
}

public function testUuidIdentifierWithSameValueDifferentInstanceDoesNotCauseViolation()
{
$uuidString = 'ec562e21-1fc8-4e55-8de7-a42389ac75c5';
$existingPerson = new UserUuidNameEntity(Uuid::fromString($uuidString), 'Foo Bar');
$this->em->persist($existingPerson);
$this->em->flush();

$dto = new UserUuidNameDto(Uuid::fromString($uuidString), 'Foo Bar', '');

$constraint = new UniqueEntity(
fields: ['fullName'],
entityClass: UserUuidNameEntity::class,
identifierFieldNames: ['id'],
em: self::EM_NAME,
);

$this->validator->validate($dto, $constraint);

$this->assertNoViolation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public function validate(mixed $value, Constraint $constraint): void

foreach ($constraint->identifierFieldNames as $identifierFieldName) {
$propertyValue = $this->getPropertyValue($entityClass, $identifierFieldName, current($result));
if ($fieldValues[$identifierFieldName] instanceof \Stringable) {
$fieldValues[$identifierFieldName] = (string) $fieldValues[$identifierFieldName];
}
if ($propertyValue instanceof \Stringable) {
$propertyValue = (string) $propertyValue;
}
if ($fieldValues[$identifierFieldName] !== $propertyValue) {
$entityMatched = false;
break;
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.