Skip to content

fix: stop [(...)] from throwing when its result is not a property - #292

Open
simonkundrik wants to merge 1 commit into
JSONPath-Plus:mainfrom
simonkundrik:fix/script-index-missing
Open

simonkundrik wants to merge 1 commit into
JSONPath-Plus:mainfrom
simonkundrik:fix/script-index-missing

Conversation

@simonkundrik

Copy link
Copy Markdown
Contributor

[(...)] throws TypeError: loc.indexOf is not a function when 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]}
  • results that can't be property names at all: NaN ($.o[(@.length-1)] on an object), null and true

That is the error in #181. In its example, $.stores[1:4:2][(@.length-1)].name, the script runs on each sliced store, which has no length, so it gives NaN. An undefined result already matches nothing (there is a test for it in test.api.js); this does the same for the rest.

_trace only 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, including null and true keys.

I added a test to test.examples.js next 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, tsc and the mocha suite pass and coverage of src stays at 100%. The only failures are the 2 cli tests, which can't run bin/jsonpath-cli.js on 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).

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant