Skip to content

Commit e7b4421

Browse files
authored
fix: stop considering properties marked with NotBlank(allowNull=true) as required (#6184)
Fixes #6183
1 parent 8535f9d commit e7b4421

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ private function getPropertyConstraints(
193193
*/
194194
private function isRequired(Constraint $constraint): bool
195195
{
196+
if ($constraint instanceof NotBlank && $constraint->allowNull) {
197+
return false;
198+
}
199+
196200
foreach (self::REQUIRED_CONSTRAINTS as $requiredConstraint) {
197201
if ($constraint instanceof $requiredConstraint) {
198202
return true;

tests/Fixtures/DummyValidatedEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class DummyValidatedEntity
3939
* @var string
4040
*/
4141
#[Assert\Email]
42+
#[Assert\NotBlank(allowNull: true)]
4243
public $dummyEmail;
4344

4445
/**

tests/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactoryTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ protected function setUp(): void
6969

7070
public function testCreateWithPropertyWithRequiredConstraints(): void
7171
{
72-
$propertyMetadata = (new ApiProperty())->withDescription('A dummy')->withReadable(true)->withWritable(true);
73-
$expectedPropertyMetadata = $propertyMetadata->withRequired(true);
72+
$dummyPropertyMetadata = (new ApiProperty())->withDescription('A dummy')->withReadable(true)->withWritable(true);
73+
$emailPropertyMetadata = (new ApiProperty())->withTypes(['https://schema.org/email'])->withReadable(true)->withWritable(true);
7474

7575
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
76-
$decoratedPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummy', [])->willReturn($propertyMetadata)->shouldBeCalled();
76+
$decoratedPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummy', [])->willReturn($dummyPropertyMetadata)->shouldBeCalled();
77+
$decoratedPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummyEmail', [])->willReturn($emailPropertyMetadata)->shouldBeCalled();
7778

7879
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
7980
$validatorMetadataFactory->getMetadataFor(DummyValidatedEntity::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
@@ -83,9 +84,16 @@ public function testCreateWithPropertyWithRequiredConstraints(): void
8384
$decoratedPropertyMetadataFactory->reveal(),
8485
[]
8586
);
86-
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummy');
8787

88-
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
88+
$this->assertEquals(
89+
$dummyPropertyMetadata->withRequired(true),
90+
$validatorPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummy'),
91+
);
92+
93+
$this->assertEquals(
94+
$emailPropertyMetadata->withRequired(false),
95+
$validatorPropertyMetadataFactory->create(DummyValidatedEntity::class, 'dummyEmail'),
96+
);
8997
}
9098

9199
public function testCreateWithPropertyWithNotRequiredConstraints(): void

0 commit comments

Comments
 (0)