CustomvarFlat: Fix path splitting for special chars and bracket notation

Two issues were fixed in the split regex:

1. Special characters before `.` were ignored:
The old regex only split on `.` when directly preceded by a word
character (\w), causing splits to be missed when `.` was preceded
by special characters like whitespace, / or $.
Result before: `foo /.bar.one` => `['foo /.bar', 'one']`
Result after: `foo /.bar.one` => `['foo /', 'bar', 'one']`

2. Brackets mid-word were incorrectly split:
The old regex split before any `[`, causing keys like `con[0]cat` to be
split incorrectly.
Resulti before: `con[0]cat` => `['con', '[0]cat']`
Now splits before `[` only when it's a standalone array index — meaning
`]` is followed by `.`, another `[`, or end of string.
Result after: `con[0]cat` => `['con', '[0]', 'cat']`
This commit is contained in:
Sukhwinder Dhillon 2026-05-20 13:01:59 +02:00
parent 07df145091
commit 2aa3a33962
2 changed files with 23 additions and 3 deletions

View file

@ -185,7 +185,7 @@ class CustomvarFlat extends Model
$path = array_merge(
[$realName],
$sourcePath
? preg_split('/(?<=\w|])\.|(?<!^|\.)(?=\[)/', $sourcePath)
? preg_split('/(?<=.)\.|(?<!^|\.)(?=\[\d+](?:[.\[]|$))/', $sourcePath)
: []
);
} else {

View file

@ -53,7 +53,19 @@ class CustomvarFlatTest extends TestCase
["real_list[0]","one","real_list","[\"one\",\"two\",\"three\"]"],
["[1].2.[3].4.[5].6","123456","[1].2","{\"[3].4\":{\"[5].6\":123456}}"],
["ex.ample.com","cba","ex.ample.com","\"cba\""],
["[4]","four","[4]","\"four\""]
["[4]","four","[4]","\"four\""],
[
"disks.disk /.disk_partitions",
"result",
"disks",
"{\"disk /\":{\"disk_partitions\":\"result\"}}",
],
[
"test.con[0]cat.foo[1]bar.foo[2]bar",
"result",
"test",
"{\"con[0]cat\":{\"foo[1]bar\":{\"foo[2]bar\":\"result\"}}}",
]
];
public const SPECIAL_CHAR_TEST_RESULT = [
@ -85,7 +97,15 @@ class CustomvarFlatTest extends TestCase
]
],
"ex.ample.com" => "cba",
"[4]" => "four"
"[4]" => "four",
"disks" => [
"disk /" => ["disk_partitions" => "result"]
],
"test" => [
"con[0]cat" => [
"foo[1]bar" => ["foo[2]bar" => "result"]
]
]
];
public function testUnflatteningOfEmptyCustomVariables()