Skip to content
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

[PropertyAccess] Allow escaping in PropertyPath #49331

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/PropertyAccess/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Allow escaping `.` and `[` with `\` in `PropertyPath`

6.2
---

Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/PropertyAccess/PropertyPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(self|string $propertyPath)
$remaining = $propertyPath;

// first element is evaluated differently - no leading dot for properties
$pattern = '/^(([^\.\[]++)|\[([^\]]++)\])(.*)/';
$pattern = '/^(((?:[^\\\\.\[]|\\\\.)++)|\[([^\]]++)\])(.*)/';

while (preg_match($pattern, $remaining, $matches)) {
if ('' !== $matches[2]) {
Expand All @@ -114,11 +114,15 @@ public function __construct(self|string $propertyPath)
$this->isNullSafe[] = false;
}

$element = preg_replace('/\\\([.[])/', '$1', $element);
if (str_ends_with($element, '\\\\')) {
$element = substr($element, 0, -1);
}
$this->elements[] = $element;

$position += \strlen($matches[1]);
$remaining = $matches[4];
$pattern = '/^(\.([^\.|\[]++)|\[([^\]]++)\])(.*)/';
$pattern = '/^(\.((?:[^\\\\.\[]|\\\\.)++)|\[([^\]]++)\])(.*)/';
}

if ('' !== $remaining) {
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/PropertyAccess/Tests/PropertyPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ public function testGetParentWithDot()
$this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
}

public function testGetElementsWithEscapedDot()
{
$propertyPath = new PropertyPath('grandpa\.parent.child');

$this->assertEquals(['grandpa.parent', 'child'], $propertyPath->getElements());
}

public function testGetElementsWithEscapedArray()
{
$propertyPath = new PropertyPath('grandpa\[parent][child]');

$this->assertEquals(['grandpa[parent]', 'child'], $propertyPath->getElements());
}

public function testGetElementsWithDoubleEscapedDot()
{
$propertyPath = new PropertyPath('grandpa\\\.par\ent.\\\child');

$this->assertEquals(['grandpa\\', 'par\ent', '\\\child'], $propertyPath->getElements());
}

public function testGetElementsWithDoubleEscapedArray()
{
$propertyPath = new PropertyPath('grandpa\\\[par\ent][\\\child]');

$this->assertEquals(['grandpa\\', 'par\ent', '\\\child'], $propertyPath->getElements());
}

public function testGetParentWithIndex()
{
$propertyPath = new PropertyPath('grandpa.parent[child]');
Expand Down