Skip to content

The JsonPath path builder produces an invalid JsonPath expressions for keys that are not valid identifiers #60664

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

Closed
stof opened this issue Jun 3, 2025 · 2 comments · Fixed by #60668

Comments

@stof
Copy link
Member

stof commented Jun 3, 2025

Symfony version(s) affected

7.3.0

Description

In JsonPath, the compact dot notation for segments is a shortcut making it easier to access keys that can be written as an identifier. The canonical form is to use the bracket notation with the property name as a (quoted) string: https://www.rfc-editor.org/rfc/rfc9535#name-segments

However, our JsonPath class building an expression is always using the dot notation, without checking that the provided keys is a valid identifier.

https://github.com/symfony/symfony/blob/v7.3.0/src/Symfony/Component/JsonPath/JsonPath.php#L33

How to reproduce

use Symfony\Component\JsonPath\JsonCrawler;
use Symfony\Component\JsonPath\JsonPath;

$jsonPathExpression = (string) (new JsonPath)->key('some-prop');

dump((new JsonCrawler('{"some-prop": "example value"}'))->find($jsonPathExpression));

Possible Solution

We should either always use the bracket notation or select the notation based on the key.

Additional Context

No response

@stof
Copy link
Member Author

stof commented Jun 3, 2025

/cc @alexandre-daubois

@alexandre-daubois
Copy link
Member

Thank you, working on a fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants