Fix - Prevent TypeError in CheckboxesField::regex() when stored value is invalid JSON#3647
Fix - Prevent TypeError in CheckboxesField::regex() when stored value is invalid JSON#3647RomainLvr wants to merge 1 commit into
Conversation
|
|
||
| public function regex($value): bool { | ||
| return (preg_grep($value, $this->value)) ? true : false; | ||
| return preg_match($value, $this->value) ? true : false; |
There was a problem hiding this comment.
regex() behavior changed (preg_grep → preg_match), but tests/3-unit/GlpiPlugin/Formcreator/Field/LdapSelectField.php has no test for regex() at all, before or after this fix. Can you add a case mirroring providerRegex in CheckboxesField.php?
| $decoded = ($value !== null && $value !== '') | ||
| ? json_decode($value) | ||
| : []; | ||
| $this->value = is_array($decoded) ? $decoded : []; |
There was a problem hiding this comment.
providerRegex covers null, '', invalid JSON string, and the JSON null literal, but not other valid-JSON-non-array values (e.g. '"a string"', '42', '{"a":1}') that also hit the is_array($decoded) ? $decoded : [] branch. Not required given the generic array check, but worth one more case for full branch coverage.
| yield 'null-decoded json value does not throw and returns false' => [ | ||
| 'value' => 'null', | ||
| 'pattern' => '/foo/', | ||
| 'expected' => false, | ||
| ]; |
There was a problem hiding this comment.
providerRegex doesn't cover valid-JSON non-array values (e.g. '"a string"', '42', '{"a":1}') that also reach the is_array($decoded) ? $decoded : [] fallback branch.
| yield 'null-decoded json value does not throw and returns false' => [ | |
| 'value' => 'null', | |
| 'pattern' => '/foo/', | |
| 'expected' => false, | |
| ]; | |
| yield 'null-decoded json value does not throw and returns false' => [ | |
| 'value' => 'null', | |
| 'pattern' => '/foo/', | |
| 'expected' => false, | |
| ]; | |
| yield 'valid json non-array value does not throw and returns false' => [ | |
| 'value' => '"a string"', | |
| 'pattern' => '/foo/', | |
| 'expected' => false, | |
| ]; |
Changes description
Checklist
Please check if your PR fulfills the following specifications:
Description
When rendering the
plugin_formcreator_solved_issuesdashboard widget, aTypeErroris thrown:preg_grep(): Argument #2 ($array) must be of type array, null givenThis happens when a form answer contains a checkbox field with a regex visibility condition, and the stored JSON value is corrupted or invalid.
json_decode()returnsnullon invalid input, leaving$this->value = null, which then causespreg_grep()to throw.A secondary bug was also found in
LdapselectField::regex(), which incorrectly usedpreg_grep()on a plain string value instead ofpreg_match().Changes
CheckboxesField::deserializeValue(): addis_array()guard afterjson_decode()so$this->valuealways ends up as an array, even when the stored data is malformed. This mirrors the existing pattern inActorField.LdapselectField::regex(): replacepreg_grep()withpreg_match(), consistent withRadiosFieldwhich holds a single string value.tests/CheckboxesField.php: addtestRegex()covering valid match, no match, invalid JSON, and JSON-null cases.