Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[Validator] Expression language allow null#41329
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
16c2086
to44c0094
Compare45769db
to18aa137
Comparecarsonbot commentedMay 21, 2021
Hey! I think@Andrej-in-ua has recently worked with this code. Maybe they can help review this? Cheers! Carsonbot |
Looks like correct fix. Thanks! |
Thanks for your review 🙂 |
derrabus commentedMay 23, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I agree that the validator should behave as you're suggesting. However, changing its behavior is technically a BC break, so we should probably add a config flag and a deprecation layer for your change. |
mpiot commentedMay 25, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@derrabus I've added an For the deprecation I don't really know ho to add it correctly. For the moment just add a Deprecation when using |
df753f7
to10218cf
CompareThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Sorry for stalling here. I have a comment about the deprecation layer. We should make sure that the deprecation supports our upgrade path to 6.0.
Also, when testing the deprecated behavior, please make sure to add it to the legacy group:
/** * @group legacy */publicfunction testSomeOldBehavior(){
This way, the deprecation will not cause the test to fail and we know which tests can be removed on 6.0.
if (isset($options['allowNullAndEmptyString'])) { | ||
trigger_deprecation('symfony/validator', '5.4', sprintf('The "allowNullAndEmptyString" option of the "%s" constraint is deprecated.', self::class)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This parameter is our forward compatibility layer and it allows us to opt-in to the behavior we want to have in 6.0. An application that does not explicitly enable that new behavior will potentially break when upgrading to Symfony 6.
What we should deprecate is not explicitly setting it totrue
.
if (isset($options['allowNullAndEmptyString'])) { | |
trigger_deprecation('symfony/validator','5.4',sprintf('The"allowNullAndEmptyString"optionof the "%s" constraint is deprecated.',self::class)); | |
} | |
if (!$this->allowNullAndEmptyString) { | |
trigger_deprecation('symfony/validator','5.4','Not setting"allowNullAndEmptyString" of the "%s" constraintto "true"is deprecated.',__CLASS__); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for that PR. What about fixinghttps://github.com/symfony/symfony/blob/5.4/src/Symfony/Component/Validator/Constraints/ExpressionValidator.php#L42 as well, could be done in the same PR I guess.
$this->allowNullAndEmptyString = $allowNullAndEmptyString ?? $this->allowNullAndEmptyString; | ||
if (!$this->allowNullAndEmptyString) { | ||
trigger_deprecation('symfony/validator', '5.4', 'Not setting "allowNullAndEmptyString" of the "%s" constraint to "true" is deprecated.', __CLASS__); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I propose to be more explicit on the upgrade path here, something likeValidating empty expressions with "%s" constraint is deprecated. Set "allowNullAndEmptyString" option to "true" instead and add explicit constraint like NotNull or NotBlank.'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@HeahDude I think you're right, I"ve just base the message on other validator that do the same. But be more explicit is maybe better for final user.
@@ -39,6 +39,10 @@ public function validate($expression, Constraint $constraint): void | |||
throw new UnexpectedTypeException($constraint, ExpressionLanguageSyntax::class); | |||
} | |||
if (true === $constraint->allowNullAndEmptyString && (null === $expression || '' === $expression)) { | |||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
The deprecation should be triggered here instead of the constraint constructor to ensure runtime context, otherwise it may be lost in the app cache warm up, something like:
if (null ===$expression ||'' ===$expression) {if ($constraint->allowNullAndEmptyString) {return; }trigger_deprecation(...);}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'll wait for other comments about it, because all triggered deprecations in Constraints are in the Constraint class and not in the Validator (eg: Length, Range)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would say that it should never be the case, otherwise it may end up assymfony/symfony-docs#14785 (refsymfony/symfony-docs#14785 (comment)).
I agree with that assessment. However, it's the current behavior of stable Symfony releases, even if it's an unexpected one. If we simply change it, we might break existing validation logic of apps. We've had a similar situation with the |
Would be great to have this in 5.4. |
7ec63ba
to15fac91
CompareI've rebase on 5.4 branch, but change nothing in the code (divergence in the discussion). |
jderusse left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Ok for me (just a minor comment about BC),
Then in 6.1 we could deprecate that new param
public function __construct(array $options = null, string $message = null, string $service = null, array $allowedVariables = null, array $groups = null, $payload = null) | ||
public function __construct(array $options = null, string $message = null, string $service = null, array $allowedVariables = null,bool $allowNullAndEmptyString = null,array $groups = null, $payload = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Shouldn't we move the parameter at the end for BC? (I wonder if it matters for annotations 🤔)
6.0 cannot contain deprecations, any deprecation should be in 6.1 instead. |
mpiot commentedNov 26, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Can we can add the deprecation message in 5.4, and remove it and the alternative logic for 6.0 branch (2nd PR) ? |
it is too late to add new deprecations in 5.4. We are already in RC. the feature freeze was more than 1 month ago. |
I think adding argument Instead of the current approach which is trying to fix a broken behavior of @mpiot up to implement this? |
mpiot commentedFeb 26, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@nicolas-grekas yes, I can create a new validator instead of update this one and deprecate the current validator. Just a question about the part: What is the way to “create” this part ? UUID ? |
Replaced by#45623 |
…ntax", use "ExpressionSyntax" instead (mpiot)This PR was merged into the 6.1 branch.Discussion----------[Validator] Deprecate constraint "ExpressionLanguageSyntax", use "ExpressionSyntax" instead| Q | A| ------------- | ---| Branch? | 6.1| Bug fix? | yes| New feature? | yes| Deprecations? | yes| Tickets | Fix| License | MIT| Doc PR |Like other validators, ExpressionLanguageSyntax validator should be ignored when the value is null or receive an empty string, add NotNull or NotBlank if needed. In#41329 `@nicolas`-grekas proposed to create a new one called `ExpressionSyntax` and deprecate `ExpressionLanguageSyntax` validator.Commits-------ceb94ca [Validator] Deprecate constraint "ExpressionLanguageSyntax", use "ExpressionSyntax" instead
Uh oh!
There was an error while loading.Please reload this page.
Like other validators, ExpressionLanguageSyntax validator should be ignored when the value is null or receive an empty string, add NotNull or NotBlank if needed.