Skip to content

Compound queries do not evaluate false expressions properly #145

Open
@gongfarmer

Description

@gongfarmer

When a compound query evaluates down to the expression true && false && true, the result is true.
It should be false.

Example:

$ cat data.json
{
  "data": [
    {
      "hasProperty1": true,
      "hasProperty2": false,
      "name": "testname1"
    },
    {
      "hasProperty1": false,
      "hasProperty2": true,
      "name": "testname2"
    },
    {
      "hasProperty1": true,
      "hasProperty2": true,
      "name": "testname3"
    }
  ]
}

$ ruby -rjson -rjsonpath -e 'data = JSON.parse(IO.read(ARGV.first)); pp JsonPath.new(ARGV[1]).on(data)' data.json '$.data[?( @.hasProperty1 && @.hasProperty2 && @.hasProperty1 )].name'
["testname1", "testname3"]

Expected result: only ["testname3"]

Root cause:
In "parser.rb", method "#parse_parentheses", the sub-expressions have been evaluated and replaced with "true" or "false".
These values are then evaluated against the operators && or ||.
The first of the compound values has its "true" or "false" string passed to #bool_or_exp which converts it to a boolean value.
However the remaining "true" or "false" strings are never evaluated and are passed to &&= or ||= directly, where they are always treated as a true result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions