Skip to content

[Validator] handle edge cases when constructing constraints with named arguments #54758

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
12 changes: 12 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ protected function newConstraint(string $name, mixed $options = null): Constrain
}

if ($this->namedArgumentsCache[$className] ??= (bool) (new \ReflectionMethod($className, '__construct'))->getAttributes(HasNamedArguments::class)) {
if (null === $options) {
return new $className();
}

if (!\is_array($options)) {
return new $className($options);
}

if (1 === \count($options) && isset($options['value'])) {
return new $className($options['value']);
}

return new $className(...$options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
foreach ($nodes as $node) {
if (\count($node) > 0) {
if (\count($node->value) > 0) {
$options = $this->parseValues($node->value);
$options = [
'value' => $this->parseValues($node->value),
];
} elseif (\count($node->constraint) > 0) {
$options = $this->parseConstraints($node->constraint);
} elseif (\count($node->option) > 0) {
Expand All @@ -94,6 +96,10 @@ protected function parseConstraints(\SimpleXMLElement $nodes): array
$options = null;
}

if (isset($options['groups']) && !\is_array($options['groups'])) {
$options['groups'] = (array) $options['groups'];
}

$constraints[] = $this->newConstraint((string) $node['name'], $options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ protected function parseNodes(array $nodes): array
$options = $this->parseNodes($options);
}

if (null !== $options && (!\is_array($options) || array_is_list($options))) {
$options = [
'value' => $options,
];
}

$values[] = $this->newConstraint(key($childNodes), $options);
} else {
if (\is_array($childNodes)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\Mapping\Loader\Fixtures;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

class ConstraintWithNamedArguments extends Constraint
{
public $choices;

#[HasNamedArguments]
public function __construct(array|string|null $choices = [], ?array $groups = null)
{
parent::__construct([], $groups);

$this->choices = $choices;
}

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
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\Component\Validator\Tests\Mapping\Loader\Fixtures;

use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;

class ConstraintWithoutValueWithNamedArguments extends Constraint
{
#[HasNamedArguments]
public function __construct(?array $groups = null)
{
parent::__construct([], $groups);
}

public function getTargets(): string
{
return self::CLASS_CONSTRAINT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments;

class XmlFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -66,6 +68,9 @@ public function testLoadClassMetadata()
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(['Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback']));
$expected->addConstraint(new Traverse(false));
$expected->addConstraint(new ConstraintWithNamedArguments('foo'));
$expected->addConstraint(new ConstraintWithNamedArguments(['foo', 'bar']));
$expected->addConstraint(new ConstraintWithoutValueWithNamedArguments(['foo']));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(['min' => 3]));
$expected->addPropertyConstraint('firstName', new Choice(['A', 'B']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use Symfony\Component\Validator\Tests\Fixtures\Entity_81;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\GroupSequenceProviderEntity;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments;
use Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments;

class YamlFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -109,6 +111,9 @@ public function testLoadClassMetadata()
$expected->addConstraint(new Callback('validateMe'));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(['Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback']));
$expected->addConstraint(new ConstraintWithoutValueWithNamedArguments());
$expected->addConstraint(new ConstraintWithNamedArguments('foo'));
$expected->addConstraint(new ConstraintWithNamedArguments(['foo', 'bar']));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(['min' => 3]));
$expected->addPropertyConstraint('firstName', new Choice(['A', 'B']));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
false
</constraint>

<!-- Constraint with named arguments support with scalar value -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
foo
</constraint>

<!-- Constraint with named arguments support with array value -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments">
<value>foo</value>
<value>bar</value>
</constraint>

<!-- Constraint with named arguments support with exactly one group -->
<constraint name="Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments">
<option name="groups">foo</option>
</constraint>

<!-- PROPERTY CONSTRAINTS -->

<property name="firstName">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity:
- Callback: validateMe
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
# Constraint with named arguments support without value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithoutValueWithNamedArguments: ~
# Constraint with named arguments support with scalar value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: foo
# Constraint with named arguments support with array value
- Symfony\Component\Validator\Tests\Mapping\Loader\Fixtures\ConstraintWithNamedArguments: [foo, bar]

properties:
firstName:
Expand Down
Loading