The Wayback Machine - https://web.archive.org/web/20231128090616/https://github.com/symfony/symfony/pull/52722
Skip to content
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

[Validator] Fix type error for non-array items when Unique::fields is set #52722

Open
wants to merge 1 commit into
base: 6.3
Choose a base branch
from

Conversation

aprat84
Copy link
Contributor

@aprat84 aprat84 commented Nov 24, 2023

Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

The UniqueValidator crashes if:

  • fields argument is set
  • value is an array but some of the elements are not

Example value:

$value = [
    [
        "field": 1
    ],
    "abc",
];

Example constraint config:

new Unique(fields: ['field']);

Error thrown:

Symfony\Component\Validator\Constraints\UniqueValidator::reduceElementKeys(): Argument #2 ($element) must be of type array, string given, called in /var/www/html/vendor/symfony/validator/Constraints/UniqueValidator.php on line 46

Solution:

Looking at the actual behavior, if one of its items, being an array, has none of the fields, is ignored.

So, continuing on this premise, if one of its items, is not an array, it should be ignored too.

That's what this PR is about (hope so)!

@carsonbot carsonbot added this to the 6.3 milestone Nov 24, 2023
@OskarStark OskarStark changed the title [Validator] Fix type error for non-array items when Unique::fields is set [Validator] Fix type error for non-array items when Unique::fields is set Nov 24, 2023
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Can you please rebase to get rid of the first commit?

@aprat84 aprat84 force-pushed the fix/unique-validator-error branch 4 times, most recently from 7071258 to 7ee329e Compare November 27, 2023 09:42
@@ -43,7 +43,7 @@ public function validate(mixed $value, Constraint $constraint)
$collectionElements = [];
$normalizer = $this->getNormalizer($constraint);
foreach ($value as $element) {
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) {
if ($fields && !(\is_array($element) && $element = $this->reduceElementKeys($fields, $element))) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we shouldn't rather throw an UnexpectedValueException instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants