joshbuddy / jsonpath

Ruby implementation of http://goessner.net/articles/JsonPath/
MIT License
447 stars 71 forks source link

Not equals (!=) operator broken unless preceded by space #147

Closed gongfarmer closed 2 years ago

gongfarmer commented 2 years ago

This is a regression in jsonpath v1.1.1 caused by my earlier commit 083d858.

Example:

   98     # Should be the same regardless of key style ( @.key vs @['key'] )
   99     result = ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien', 'Lukyanenko']
  100     assert_equal result, JsonPath.new("$..book[?(@['title']=='Osennie Vizity' || @['author']!='Lukyanenko')].author").on(@object)
  101     assert_equal result, JsonPath.new("$..book[?(@.title=='Osennie Vizity' || @.author != Lukyanenko )].author").on(@object)
  102     assert_equal result, JsonPath.new("$..book[?(@.title=='Osennie Vizity' || @.author!=Lukyanenko )].author").on(@object)

The test on line 102 fails, despite the fact that the query only differs from line 101 by whitespace.

Expected output: ['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien', 'Lukyanenko'] Actual output: ["Lukyanenko"]

gongfarmer commented 2 years ago

I don't recommend restoring the original code that I removed. Reasons:

Instead this PR changes the parser code which is eagerly consuming the ! character too early.

I considered that this might break handling of keys that contain a ! character, for example a query like this:

{
  "data": [
    {
        "name!": "testname1"
    }
  ]
}

JsonPath.new("$.data[?(@['name!']==testname1)]").on(data)

However this query returns no results on v1.1.0, v1.1.1 and this does not change with my new PR.

joshbuddy commented 2 years ago

Yeah, makes total sense to me. It would be good to fix ! in the key names at some point, but given the already broken behavior and the fact this situation probably rarely comes up, its good to proceed with your PR and release (which is what i've done) and address this at some future point.

Thanks again for your work on this!