Repository navigation
fix: stop [(...)] from throwing when its result is not a property - #292
Open
simonkundrik wants to merge 1 commit into
Open
simonkundrik wants to merge 1 commit into
simonkundrik wants to merge 1 commit into
Conversation
A script expression that evaluated to a number or other non-string value which isn't a property of the current value threw "loc.indexOf is not a function". The README's idiom for the last item, $..book[(@.length-1)], hit this on any empty array (the result is -1), so a single empty array anywhere in the tree made the whole query throw. Results such as NaN, null and true did the same. An undefined result already matched nothing. _trace only follows a non-string location when it is an own property of the value, and every later branch calls string methods on it. A location that isn't such a property now matches nothing. Fixes JSONPath-Plus#181
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[(...)]throwsTypeError: loc.indexOf is not a functionwhen the script result isn't a string and isn't a property of the current value. The README's idiom for the last item,$..book[(@.length-1)], hits it on any empty array (the result is-1), so one empty array anywhere in the tree makes the whole query throw:$.items[(@.length-1)]on{"items": []}$..items[(@.length-1)]on{"items": [], "other": {"items": [5, 6]}}(should give[6])$.items[(@.length+5)]on{"items": [1, 2]}NaN($.o[(@.length-1)]on an object),nullandtrueThat is the error in #181. In its example,
$.stores[1:4:2][(@.length-1)].name, the script runs on each sliced store, which has nolength, so it givesNaN. Anundefinedresult already matches nothing (there is a test for it intest.api.js); this does the same for the rest._traceonly follows a non-string location when it is an own property of the value, and every branch after that calls string methods on it. A location that isn't such a property now matches nothing. Properties that do exist are still followed, includingnullandtruekeys.I added a test to
test.examples.jsnext to the existing(@.length-1)one, with the cases above, and it runs in both eval modes. It fails on main with the TypeError from the issue and passes now.eslint,tscand the mocha suite pass and coverage ofsrcstays at 100%. The only failures are the 2 cli tests, which can't runbin/jsonpath-cli.json my Windows machine, same as on main. I also compared old and new results for every path string in the test files on a few sample documents: the only ones that changed were ones that threw before.It doesn't touch the same lines as #291, and I ran the suite on the two branches merged together to check they work together (all passing, still 100% coverage).