Skip to content

Commit

Permalink
bug #54758 [Validator] handle edge cases when constructing constraint…
Browse files Browse the repository at this point in the history
…s with named arguments (xabbuh)

This PR was merged into the 6.4 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

spotted while working on #54744

Commits
-------

93eb8a0 handle edge cases when constructing constraints with named arguments
  • Loading branch information
fabpot committed May 1, 2024
2 parents 2722fb5 + 93eb8a0 commit 0c764e7
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php
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
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
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
@@ -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;
}
}
@@ -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;
}
}
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
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
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
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

0 comments on commit 0c764e7

Please sign in to comment.