Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[JsonPath] Fix support for comma separated indices #60719

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion src/Symfony/Component/JsonPath/JsonCrawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,56 @@ private function evaluateBracket(string $expr, mixed $value): array
return $this->evaluateFilter($innerExpr, $value);
}

// quoted strings for object keys
// comma-separated values, e.g. `['key1', 'key2', 123]` or `[0, 1, 'key']`
if (str_contains($expr, ',')) {
$parts = $this->parseCommaSeparatedValues($expr);

$result = [];
$keysIndices = array_keys($value);
$isList = array_is_list($value);

foreach ($parts as $part) {
$part = trim($part);

if (preg_match('/^([\'"])(.*)\1$/', $part, $matches)) {
$key = JsonPathUtils::unescapeString($matches[2], $matches[1]);

if ($isList) {
foreach ($value as $item) {
if (\is_array($item) && \array_key_exists($key, $item)) {
$result[] = $item;
break;
}
}

continue; // no results here
}

if (\array_key_exists($key, $value)) {
$result[] = $value[$key];
}
} elseif (preg_match('/^-?\d+$/', $part)) {
// numeric index
$index = (int) $part;
if ($index < 0) {
$index = \count($value) + $index;
}

if ($isList && \array_key_exists($index, $value)) {
$result[] = $value[$index];
continue;
}

// numeric index on a hashmap
if (isset($keysIndices[$index]) && isset($value[$keysIndices[$index]])) {
$result[] = $value[$keysIndices[$index]];
}
}
}

return $result;
}

if (preg_match('/^([\'"])(.*)\1$/', $expr, $matches)) {
$key = JsonPathUtils::unescapeString($matches[2], $matches[1]);

Expand Down Expand Up @@ -415,4 +464,44 @@ private function compare(mixed $left, mixed $right, string $operator): bool
default => false,
};
}

private function parseCommaSeparatedValues(string $expr): array
{
$parts = [];
$current = '';
$inQuotes = false;
$quoteChar = null;

for ($i = 0; $i < \strlen($expr); ++$i) {
$char = $expr[$i];

if ('\\' === $char && $i + 1 < \strlen($expr)) {
$current .= $char.$expr[++$i];
continue;
}

if ('"' === $char || "'" === $char) {
if (!$inQuotes) {
$inQuotes = true;
$quoteChar = $char;
} elseif ($char === $quoteChar) {
$inQuotes = false;
$quoteChar = null;
}
} elseif (!$inQuotes && ',' === $char) {
$parts[] = trim($current);
$current = '';

continue;
}

$current .= $char;
}

if ('' !== $current) {
$parts[] = trim($current);
}

return $parts;
}
}
29 changes: 29 additions & 0 deletions src/Symfony/Component/JsonPath/Tests/JsonCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ public function testEscapedDoubleQuotesInFieldName()
$this->assertSame(42, $result[0]);
}

public function testMultipleKeysAtOnce()
{
$crawler = new JsonCrawler(<<<JSON
{"a": {"b\\"c": 42}, "b": {"c": 43}}
JSON);

$result = $crawler->find("$['a', 'b', 3]");

$this->assertSame([
['b"c' => 42],
['c' => 43],
], $result);
}

public function testMultipleKeysAtOnceOnArray()
{
$crawler = new JsonCrawler(<<<JSON
[{"a": 1}, {"b": 2}, {"c": 3}, {"a,b,c": 5}, {"d": 4}]
JSON);

$result = $crawler->find("$[0, 2, 'a,b,c', -1]");

$this->assertCount(4, $result);
$this->assertSame(['a' => 1], $result[0]);
$this->assertSame(['c' => 3], $result[1]);
$this->assertSame(['a,b,c' => 5], $result[2]);
$this->assertSame(['d' => 4], $result[3]);
}

public function testBasicNameSelector()
{
$result = self::getBookstoreCrawler()->find('$.store.book')[0];
Expand Down
Loading