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

Appearance settings

[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 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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ class ExpressionLanguageSyntax extends Constraint
public $message = 'This value should be a valid expression.';
public $service;
public $allowedVariables;
public $allowNullAndEmptyString = false;

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)
Copy link
Member

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 🤔)

{
parent::__construct($options, $groups, $payload);

$this->message = $message ?? $this->message;
$this->service = $service ?? $this->service;
$this->allowedVariables = $allowedVariables ?? $this->allowedVariables;
$this->allowNullAndEmptyString = $allowNullAndEmptyString ?? $this->allowNullAndEmptyString;

if (!$this->allowNullAndEmptyString) {
trigger_deprecation('symfony/validator', '5.4', 'Validating empty expressions with "%s" constraint is deprecated. Set "allowNullAndEmptyString" option to "true" instead and add explicit constraint like NotNull or NotBlank.', __CLASS__);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

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(...);
}

Copy link
Contributor Author

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)

Copy link
Contributor

@HeahDude HeahDude Aug 27, 2021

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 as symfony/symfony-docs#14785 (ref symfony/symfony-docs#14785 (comment)).

}

if (!\is_string($expression)) {
throw new UnexpectedValueException($expression, 'string');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function testAttributes()
[$aConstraint] = $metadata->properties['a']->getConstraints();
self::assertNull($aConstraint->service);
self::assertNull($aConstraint->allowedVariables);
self::assertFalse($aConstraint->allowNullAndEmptyString);

[$bConstraint] = $metadata->properties['b']->getConstraints();
self::assertSame('my_service', $bConstraint->service);
Expand All @@ -70,6 +71,9 @@ public function testAttributes()
[$cConstraint] = $metadata->properties['c']->getConstraints();
self::assertSame(['foo', 'bar'], $cConstraint->allowedVariables);
self::assertSame(['my_group'], $cConstraint->groups);

[$dConstraint] = $metadata->properties['d']->getConstraints();
self::assertTrue($dConstraint->allowNullAndEmptyString);
}
}

Expand All @@ -83,4 +87,7 @@ class ExpressionLanguageSyntaxDummy

#[ExpressionLanguageSyntax(allowedVariables: ['foo', 'bar'], groups: ['my_group'])]
private $c;

#[ExpressionLanguageSyntax(allowNullAndEmptyString: true)]
private $d;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,46 @@ public function testExpressionIsNotValid()
->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR)
->assertRaised();
}

public function testNullIsValid()
{
$this->validator->validate(null, new ExpressionLanguageSyntax([
'allowNullAndEmptyString' => true,
]));

$this->assertNoViolation();
}

/**
* @group legacy
*/
public function testNullWithoutAllowOptionIsNotValid()
{
$this->expectExceptionMessage('Expected argument of type "string", "null" given');

$this->validator->validate(null, new ExpressionLanguageSyntax());
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new ExpressionLanguageSyntax([
'allowNullAndEmptyString' => true,
]));

$this->assertNoViolation();
}

/**
* @group legacy
*/
public function testEmptyStringWithoutAllowOptionIsNotValid()
{
$this->validator->validate('', new ExpressionLanguageSyntax());

$this->buildViolation('This value should be a valid expression.')
->setParameter('{{ syntax_error }}', '"Unexpected token "end of expression" of value "" around position 1."')
->setInvalidValue('')
->setCode(ExpressionLanguageSyntax::EXPRESSION_LANGUAGE_SYNTAX_ERROR)
->assertRaised();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.