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

Skip to content

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

Closed
@stof

Description

@stof

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions