Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
xabbuh wants to merge1 commit intosymfony:5.4fromxabbuh:pr-53133

Conversation

@xabbuh
Copy link
Member

@xabbuhxabbuh commentedJan 6, 2024
edited
Loading

QA
Branch?5.4
Bug fix?yes
New feature?no
Deprecations?no
IssuesFix#53133 (comment),Fix#53455
LicenseMIT

returnfalse;
}

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

@tscnitscniJan 6, 2024
edited
Loading

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 =newCollection(    ['foo' => [// implicit "new Required(...)"            [newType('string'),            ],        ],    ],);$c =newCollection(    ['foo' => [// implicit "new Required(...)"'groups' => ['bar'],// no constraint in first value'constraints' => [newType('string'),            ],        ],    ],);

Copy link
MemberAuthor

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
MemberAuthor

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

@tscnitscniJan 6, 2024
edited
Loading

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

newCollection(    fields: ['foo' =>newRequired([newType('string'),        ]),    ],);// more explicitnewCollection(    fields: ['foo' =>newRequired(            options:  ['constraints' => [newType('string')                ],            ],            groups: ['bar'],        ),    ],);

Copy link
Member

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 =newCollection(    ['foo' => [            [newType('string'),            ],        ],    ],);$c2 =newCollection(    ['foo' => [newType('string'),        ],    ],);$c3 =newCollection(    ['foo' =>newType('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 (!$fieldinstanceof Optional && !$fieldinstanceof Required) {
$this->fields[$fieldName] =newRequired($field);
}

So when it gets to line 76, it's eithernew Required([new Type('string')]) ornew Required(new Type('string')), and sinceRequired 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
MemberAuthor

Status: Needs work

@HypeMC
Copy link
Member

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

axzx reacted with thumbs up emoji

@xabbuh
Copy link
MemberAuthor

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

HypeMC reacted with thumbs up emoji

@HypeMC
Copy link
Member

@xabbuh See#53755

@xabbuh
Copy link
MemberAuthor

closing in favor of#53755

@xabbuhxabbuh closed thisFeb 4, 2024
@xabbuhxabbuh deleted the pr-53133 branchFebruary 4, 2024 17:01
nicolas-grekas added a commit that referenced this pull requestFeb 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       | MITContinuation 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@HypeMCHypeMCHypeMC left review comments

+1 more reviewer

@tscnitscnitscni left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

5.4

Development

Successfully merging this pull request may close these issues.

4 participants

@xabbuh@HypeMC@tscni@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp