Skip to content

[Validator] deal with fields for which no constraints have been configured #53443

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

Closed
wants to merge 1 commit into from

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Jan 6, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #53133 (comment), Fix #53455
License MIT

return false;
}

if ([] !== $optionOrField && !($optionOrField[0] ?? null) instanceof Constraint) {
Copy link
Contributor

@tscni tscni Jan 6, 2024

Choose a reason for hiding this comment

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

In theory this would still break constructions like these

$c = new Collection(
    [
        'foo' => [  // implicit "new Required(...)"
            [
                new Type('string'),
            ],
        ],
    ],
);
$c = new Collection(
    [
        'foo' => [  // implicit "new Required(...)"
            'groups' => ['bar'], // no constraint in first value
            'constraints' => [
                new Type('string'),
            ],
        ],
    ],
);

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure I understand what you mean. Both examples where already invalid before, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant that they originally worked before #53133

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you sure? How were both forms supposed to end up?

Copy link
Contributor

@tscni tscni Jan 6, 2024

Choose a reason for hiding this comment

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

Yes, both work on e.g. 6.4.0. That 0 index access seemed suspicous to me, so I tried to break it a bit.
(Note that I don't care about this working, but thought it worthwhile to point out)

The examples above would turn into

new Collection(
    fields: [
        'foo' => new Required([
            new Type('string'),
        ]),
    ],
);
// more explicit
new Collection(
    fields: [
        'foo' => new Required(
            options:  [
                'constraints' => [
                    new Type('string')
                ],
            ],
            groups: ['bar'],
        ),
    ],
);

Copy link
Contributor

Choose a reason for hiding this comment

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

After doing some testing, I don't think it makes sense to support this case, here's why:

$c1 = new Collection(
    [
        'foo' => [
            [
                new Type('string'),
            ],
        ],
    ],
);
$c2 = new Collection(
    [
        'foo' => [
            new Type('string'),
        ],
    ],
);
$c3 = new Collection(
    [
        'foo' => new Type('string'),
    ],
);

All three cases result in:

array (
  'foo' => 
  \Symfony\Component\Validator\Constraints\Required::__set_state(array(
     'payload' => NULL,
     'groups' => 
    array (
      0 => 'Default',
    ),
     'constraints' => 
    array (
      0 => 
      \Symfony\Component\Validator\Constraints\Type::__set_state(array(
         'payload' => NULL,
         'groups' => 
        array (
          0 => 'Default',
        ),
         'message' => 'This value should be of type {{ type }}.',
         'type' => 'string',
      )),
    ),
  )),
)

The reason why the first example works is because of the following code:

// the XmlFileLoader and YamlFileLoader pass the field Optional
// and Required constraint as an array with exactly one element
if (\is_array($field) && 1 == \count($field)) {
$this->fields[$fieldName] = $field = $field[0];
}
if (!$field instanceof Optional && !$field instanceof Required) {
$this->fields[$fieldName] = new Required($field);
}

So when it gets to line 76, it's either new Required([new Type('string')]) or new Required(new Type('string')), and since Required casts non arrays to arrays, the result is the same:

if (!\is_array($nestedConstraints)) {
$nestedConstraints = [$nestedConstraints];
}

So my conclusion is that this used to work by accident, and was really not intended to work.

@xabbuh
Copy link
Member Author

xabbuh commented Jan 8, 2024

Status: Needs work

@HypeMC
Copy link
Contributor

HypeMC commented Jan 30, 2024

@xabbuh Do you need any help finishing this one? I feel responsible cause it was my PR that caused the issues 😄

@xabbuh
Copy link
Member Author

xabbuh commented Jan 31, 2024

@HypeMC Please do if you have the time for it. :)

@HypeMC
Copy link
Contributor

HypeMC commented Feb 4, 2024

@xabbuh See #53755

@xabbuh
Copy link
Member Author

xabbuh commented Feb 4, 2024

closing in favor of #53755

@xabbuh xabbuh closed this Feb 4, 2024
@xabbuh xabbuh deleted the pr-53133 branch February 4, 2024 17:01
nicolas-grekas added a commit that referenced this pull request Feb 9, 2024
… (xabbuh, HypeMC)

This PR was merged into the 5.4 branch.

Discussion
----------

[Validator] Fix fields without constraints in `Collection`

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #53133 (comment), Fix #53455
| License       | MIT

Continuation of #53443.

Commits
-------

f6217d8 [Validator] Fix fields without constraints in `Collection`
b341535 deal with fields for which no constraints have been configured
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.

4 participants