Bug: Constraints check doesn't work
dramamask opened this issue · 4 comments
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Constraints checks don't work. The issue is in DefaultConstraintsValidator.php on line 33:
$valueToPass = $constraint->getValues() ?? (
method_exists($constraint, 'getSingleValue')
? $constraint->getSingleValue()
: null
);
This code doesn't work as intended because $constraints->getValues()
returns an empty array. The null coalescing operator only works for null values, not for an empty array value.
The code could be rewritten like this (to fix the issue):
$valueToPass = $constraint->getValues();
if (!$valueToPass) {
$valueToPass = $constraint->getSingleValue() ?? null;
}
To reproduce
This can be reproduced by using a DATE_BEFORE constraint on the currentTime. Just add the constraint on the default strategy. The feature toggle will evaluate to "off" as soon as you add the constraint.
Sample code (optional)
No response
Version
v1.11.082
Expected behavior
I expect the constraint to be properly evaluated. Instead it always evaluates to false.
Logs (optional)
No response
Additional context (optional)
I'm using unleash-client-php in a Laravel project, together with the j-webb/unleash package.
I am failing to reproduce this, I've tried using this in a test:
[
'version' => 1,
'features' => [
[
'name' => 'test',
'description' => '',
'enabled' => true,
'strategies' => [
[
'name' => 'default',
'constraints' => [
[
'contextName' => 'currentTime',
'operator' => ConstraintOperator::DATE_BEFORE,
'value' => (new DateTimeImmutable('+1 day'))->format('c'),
],
],
],
],
],
],
]
It evaluates to true. Can you provide a failing test?
Try these values for strategies. This is a copy of the json that I get from Unleash.
"strategies": [
{
"name": "default",
"disabled": false,
"constraints": [
{
"value": "2024-05-19T15:55:00.000Z",
"values": [],
"inverted": false,
"operator": "DATE_BEFORE",
"contextName": "currentTime",
"caseInsensitive": false
}
],
"parameters": {}
}
],
The problem lies with the "values":[]
attribute that I'm receiving from Unleash. The empty array messes things up and makes it so that the code in DefaultConstraintValidator.php
on line 33 doesn't work as expected.
It has been fixed and released in v1.11.1.
Thank you!