Skip to content

[Validator] respect groups when merging constraints #21183

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
Jan 10, 2017

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Jan 6, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #20853
License MIT
Doc PR

@peterrehm
Copy link
Contributor

On a dirst glance this still breaks the intention as if you validate Child still the parent constraints are validated as well, right? But as I understand the logic only if you apply Default as group the parent constraints are validate as well, otherwise only the constraints of the specific class are validated which is the onl way to provide full flexibility. The actual issue is that you cannot use Default as group in the GroupSequence.

@xabbuh
Copy link
Member Author

xabbuh commented Jan 6, 2017

Well, I think that merging symfony/symfony-docs#7249 was a mistake I made. At least, by looking at the rest of the code, it seems to me that validating constraints from the parent class to was intended when validating a sub class. I am not sure if I fully understand your issue. So if you had a test case of which you think it should pass, but that is still failing, I would be happy to look at it.

@peterrehm
Copy link
Contributor

Initially when documenting the inheritance I thought validating Child would validate the Parent as well.

When I tested it now again I figured out it is not like that which makes somehow sense as then you would never be able to just validate the child constraints as Default validates both, Parent just the Parent constraints and Child again both. I did extensive tests and sent a mail to Bernhard regarding the original intention but did not get any feedback.

In any case we need to be careful, the current implementation is entirely broken and a BC break which should be reverted. Your fix will still be a BC break for those relying on zhe case wheere you just want to validate the Child constraints. I do not know if that is the case in real wirld apps.

I will try your fix the next days, most likely it will fix the wrong groups issue, but I am concerned regarding the BC break.

I hope you can understand the issue now?

Did you find anything related to the inheritance? Maybe you can reach out to Bernhard?

@xabbuh
Copy link
Member Author

xabbuh commented Jan 7, 2017

When I tested it now again I figured out it is not like that which makes somehow sense as then you would never be able to just validate the child constraints as Default validates both, Parent just the Parent constraints and Child again both. I did extensive tests and sent a mail to Bernhard regarding the original intention but did not get any feedback.

But is that really a use case? The problem with this approach is that you then easily could break applications by moving class properties to a base class. Everywhere else in PHP the property would still look like it's part of the object being validated while the validator doesn't agree with this.

IMO, this is not how it should behave. It's different from cascading validation to nested objects because nested object are not really part of the object, but are just associated objects while properties from parent classes are just part of the unit we are looking at (there's noch such difference in OOP when looking at an object from the outside).

In any case we need to be careful, the current implementation is entirely broken and a BC break which should be reverted.

I agree that this is a BC break. However, I also see the issue it fixes. We just need to be careful not to blindly always add the constraint to the default group.

Did you find anything related to the inheritance?

Well, the addImplicitGroupName() method of the base Constraint class which is called here too already behaves this way. Thus, without the change from this PR, the newly added (merged) constraint would have groups whereas it wasn't party of this group in the $constraintsByGroup property.

Maybe you can reach out to Bernhard?

I'll ask him.

@peterrehm
Copy link
Contributor

Okay, I just tested it with the BC Break caused in one of my apps, this would fix it.

@yakobe Have you had the chance to test this? Does this fix your issues?

The other matter is mainly philosophic I guess. When I investigated this I while ago I think
someone told me or I tried the way how the inheritance is supposed to work. On the one
hand side I think it makes no sense that if you validate Child that Parent is not validated,
but apparently in the past it worked that way.

My personal favor would be that if you validate Child that Parent is validated as well,
this is better DX but there is then no way to validate only Child. I would accept that
because I think this seems to unveil bad code design.

It will be a BC break if this was intended previously and we shoudl discuss if we want it
that way or if this needs to wait until the next major release. Best would be to find out
Bernhards take on that.

Once decided we should then adjust the docs accordingly.

if (in_array($constraint::DEFAULT_GROUP, $constraint->groups, true)) {
$member->constraintsByGroup[$this->getDefaultGroup()][] = $constraint;
}

$constraint->addImplicitGroupName($this->getDefaultGroup());
Copy link
Contributor

Choose a reason for hiding this comment

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

Eventually you might have to call that prior to the previous if statement?

Copy link
Member Author

Choose a reason for hiding this comment

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

That shouldn't make a difference.

@yakobe
Copy link
Contributor

yakobe commented Jan 8, 2017

This works for my use case.

I also do not see a practical use case for only validating Child without Parent. But you guys are right to investigate the impacts. Thanks 👍

@peterrehm
Copy link
Contributor

@xabbuh Any news on this?

@xabbuh
Copy link
Member Author

xabbuh commented Jan 10, 2017

@peterrehm Not yet, let's see what the other @symfony/deciders think about this.

Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

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

This look like a good fix to me 👍

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Jan 10, 2017
@fabpot
Copy link
Member

fabpot commented Jan 10, 2017

Thank you @xabbuh.

@fabpot fabpot merged commit 9a60057 into symfony:2.7 Jan 10, 2017
fabpot added a commit that referenced this pull request Jan 10, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[Validator] respect groups when merging constraints

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20853
| License       | MIT
| Doc PR        |

Commits
-------

9a60057 respect groups when merging constraints
@xabbuh xabbuh deleted the issue-20853 branch January 10, 2017 15:41
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Jan 10, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[Validation] Revert #7249

This reverts commit 6c3ff5e since
due to symfony/symfony#21183 the logic is meanwhile changed.

Commits
-------

3250ed4 Revert "Fixed wrong inheritance information"
@teohhanhui
Copy link
Contributor

Symfony Validator claims to follow JSR-303, so compliance should be the priority.

@peterrehm
Copy link
Contributor

@teohhanhui What does JSR-303 say in this regard?

@teohhanhui
Copy link
Contributor

@peterrehm I'm not sure... Someone needs to take a look, and then we'll know what's the right solution.

I had to refer to the JSR-303 spec the last time, and it wasn't fun. 😆

@peterrehm
Copy link
Contributor

http://beanvalidation.org/1.0/spec/#constraintdeclarationvalidationprocess-groupsequence-formaldefinition

states:

bildschirmfoto 2017-01-25 um 20 25 58

So I think the now modified change makes it according to JSR-303.

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.

8 participants