Skip to content

[Validator] handle union and intersection types for cascaded validations #54760

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 1 commit into from
May 1, 2024
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
31 changes: 30 additions & 1 deletion src/Symfony/Component/Validator/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function addConstraint(Constraint $constraint)
$this->cascadingStrategy = CascadingStrategy::CASCADE;

foreach ($this->getReflectionClass()->getProperties() as $property) {
if ($property->hasType() && (('array' === $type = $property->getType()->getName()) || class_exists($type))) {
if ($this->canCascade($property->getType())) {
$this->addPropertyConstraint($property->getName(), new Valid());
}
}
Expand Down Expand Up @@ -511,4 +511,33 @@ private function checkConstraint(Constraint $constraint)
}
}
}

private function canCascade(?\ReflectionType $type = null): bool
{
if (null === $type) {
return false;
}

if ($type instanceof \ReflectionIntersectionType) {
foreach ($type->getTypes() as $nestedType) {
if ($this->canCascade($nestedType)) {
return true;
}
}

return false;
}

if ($type instanceof \ReflectionUnionType) {
foreach ($type->getTypes() as $nestedType) {
if (!$this->canCascade($nestedType)) {
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for intersections, shouldn't we return true if at least one can cascade ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

}
}

return true;
}

return $type instanceof \ReflectionNamedType && (\in_array($type->getName(), ['array', 'null'], true) || class_exists($type->getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Component\Validator\Tests\Fixtures;

class CascadingEntityIntersection
{
public CascadedChild&\stdClass $classes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\Component\Validator\Tests\Fixtures;

class CascadingEntityUnion
{
public CascadedChild|\stdClass $classes;
public CascadedChild|array $classAndArray;
public CascadedChild|null $classAndNull;
public array|null $arrayAndNull;
public CascadedChild|array|null $classAndArrayAndNull;
public int|string $scalars;
public int|null $scalarAndNull;
public CascadedChild|int $classAndScalar;
public array|int $arrayAndScalar;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Annotation\EntityParent;
use Symfony\Component\Validator\Tests\Fixtures\Annotation\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Fixtures\CascadingEntity;
use Symfony\Component\Validator\Tests\Fixtures\CascadingEntityIntersection;
use Symfony\Component\Validator\Tests\Fixtures\CascadingEntityUnion;
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
Expand Down Expand Up @@ -361,6 +363,40 @@ public function testCascadeConstraint()
'children',
], $metadata->getConstrainedProperties());
}

/**
* @requires PHP 8.0
*/
public function testCascadeConstraintWithUnionTypeProperties()
{
$metadata = new ClassMetadata(CascadingEntityUnion::class);
$metadata->addConstraint(new Cascade());

$this->assertSame(CascadingStrategy::CASCADE, $metadata->getCascadingStrategy());
$this->assertCount(5, $metadata->properties);
$this->assertSame([
'classes',
'classAndArray',
'classAndNull',
'arrayAndNull',
'classAndArrayAndNull',
], $metadata->getConstrainedProperties());
}

/**
* @requires PHP 8.1
*/
public function testCascadeConstraintWithIntersectionTypeProperties()
{
$metadata = new ClassMetadata(CascadingEntityIntersection::class);
$metadata->addConstraint(new Cascade());

$this->assertSame(CascadingStrategy::CASCADE, $metadata->getCascadingStrategy());
$this->assertCount(1, $metadata->properties);
$this->assertSame([
'classes',
], $metadata->getConstrainedProperties());
}
}

class ClassCompositeConstraint extends Composite
Expand Down
Loading